Thread: please help me to get some head start into this program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    please help me to get some head start into this program

    In a given string WAP to count the number of vowels (single char), consonants (single char), digits (single digit), whitespace, special symbols, words (multiple char) and numbers (multiple digits). Now sort the same strings in the ascending order of counts.
    NOTE : 1. Do not use any additional array.
    2. No inbuilt string function can be used.
    3. Use separate function for User Input, count and sorting (pass by reference).
    Sample Input/output
    Input :
    this is cp 2 for 2 year students .
    Output:
    vowels (single char) 0
    consonants (single char) 0
    digits (single digit) 2
    whitespace 7
    special symbols 1
    words (multiple char) 6


    Sorted string
    2 2 . is cp for students this year
    Last edited by tvss; 09-07-2011 at 09:16 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    actually m new to pointers....it says not to use additional arrays...i dont know how we can do without using additional array...

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tvss View Post
    actually m new to pointers....it says not to use additional arrays...i dont know how we can do without using additional array...
    Well, in that case you may find some value in Prelude's pointer tutorial.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sounds like a "state machine". The idea is you step thru the data a byte at a time, and use a variable to keep track of what's happening.

    [edit: This is for extracting data from the string. You can use a similar method if you are just counting different kinds of items, see the next few posts.]

    Code:
    #include <stdio.h>
    
    enum {
    	NONE,
    	NUMBER,
    	WORD,
    };
    
    int main() {
    	int state = NONE;
    	char input[] = "one 234 @#$garbage!! five 17\t okay", 
    		*p = input, *start = NULL;
    
    	while (*p) {
    		if (state == NONE) {
    			if (*p >= '0' && *p <= '9') state = NUMBER;
    			else if ((*p >= 'a' && *p <= 'z') 
    			  || (*p >= 'A' && *p <= 'Z')) state = WORD;
    			start = p++;
    			continue;
    		}
    		if (*p <= ' ' && start) {
    			*p = '\0';
    			if (state == NUMBER)
    				printf("number: %s\n", start);
    			else printf("word: %s\n", start);
    			state = NONE;
    		}
    		p++;
    	}
    	if (state == NUMBER)
    		printf("number: %s\n", start);
    	else if (state == WORD) printf("word: %s\n", start);
    
    	return 0;
    }
    That probably needs some elaboration, eg, notice it handles one kind of mistake but not another:

    word: one
    number: 234
    word: garbage!!
    word: five
    number: 17
    word: okay

    Hopefully the basic idea is clear. You need to understand ASCII for this:

    http://www.asciitable.com/
    Last edited by MK27; 09-07-2011 at 11:03 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tvss View Post
    actually m new to pointers....it says not to use additional arrays...i dont know how we can do without using additional array...
    Actually you don't need pointers to examin an array... whenever possible use the built in array indexing... arr[index].

    For example...
    Code:
    char string[] = "This is a test string";
    int x = strlen(string);
    int i;
    
    for (i = 0; i < x; i++)
      printf("%c", string[i];
    Pointers should not be used when simpler mechanisms exist.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Actually you don't need pointers to examin an array... whenever possible use the built in array indexing... arr[index].

    Pointers should not be used when simpler mechanisms exist.
    So you are saying this:

    Code:
    char value = string[i];
    char *address = &string[i];  <- pointer in disguise
    is simpler than:

    Code:
    char value = *p;
    char *address = p;
    I gotta disagree, but it is not much more than a matter of style. I think saying "you should not use pointers to examine an array" is an excessive caveat. What if I want to save my place (ala the "start" pointer in my previous post)? Do I then use:

    Code:
    int start;
    start = i;
    printf("%s", &string[start]);
    I guess that's fine, but I don't see how it is "simpler", and I also don't think you are avoiding pointers with that, you are just disguising them with &.

    Pointers are simple, once you understand what they are, and are one of the most fundamental and powerful tools in C programming. Beyond "hello world" they're totally essential, IMHO.
    Last edited by MK27; 09-07-2011 at 10:35 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    So you are saying this:

    Code:
    char value = string[i];
    char *address = &string[i];  <- pointer in disguise
    ... Is almost certainly unnecessary in this case.

    He can simply use array indexing to examine the string. He does not need pointers in this case... 10-4?

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    He can simply use array indexing to examine the string. He does not need pointers in this case... 10-4?
    10-99: My bad. I just noticed this is about counting, not extraction. For extraction, you must use a pointer somewhere.
    Last edited by MK27; 09-07-2011 at 11:04 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Well think about the getch() function. It returns single characters, so we don't need the string. Every time we press a button, we directly look up what it is, and raise the variable. Global, I think of somthing like this:


    ////variables
    int whitespace;
    etc.
    Code:
            while((c=getch())!=\n)
            {
                     if(c=" ")                                                         //i don't now for sure if this is the right ascii notation off a whitespace
                               whitespace++;
    }
    printf("well, you typed %d whitespaces",whitespace);
    For a capital, see your ascii tabel. Note that this is just a global idee, and the code is absolutly nonsens. It's just a example of your global architecture

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    10-99: My bad. I just noticed this is about counting, not extraction. For extraction, you must use a pointer somewhere.
    Why? char c = arr[27]; is perfectly valid...

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Libpgeak View Post
    Well think about the getch() function. It returns single characters, so we don't need the string. Every time we press a button, we directly look up what it is, and raise the variable. Global, I think of somthing like this:


    ////variables
    int whitespace;
    etc.
    Code:
            while((c=getch())!=\n)
            {
                     if(c=" ")                                                         //i don't now for sure if this is the right ascii notation off a whitespace
                               whitespace++;
    }
    printf("well, you typed %d whitespaces",whitespace);
    For a capital, see your ascii tabel. Note that this is just a global idee, and the code is absolutly nonsens. It's just a example of your global architecture
    Stay away from global variables as they can introduce problems you are not suspecting and in general are considered bad programming practice. Additionally, getch() is non standard and should be avoided since it does not produce reliable code which can be compiled on all compilers.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by Libpgeak View Post
    Well think about the getch() function. It returns single characters, so we don't need the string. Every time we press a button, we directly look up what it is, and raise the variable. Global, I think of somthing like this:


    ////variables
    int whitespace;
    etc.
    Code:
            while((c=getch())!=\n)
            {
                     if(c=" ")                                                         //i don't now for sure if this is the right ascii notation off a whitespace
                               whitespace++;
    }
    printf("well, you typed %d whitespaces",whitespace);
    For a capital, see your ascii tabel. Note that this is just a global idee, and the code is absolutly nonsens. It's just a example of your global architecture
    Careful! Your code should actually read
    Code:
    while((c=getch())!='\n')
            {
                     if(c==' ')                                                         //i don't now for sure if this is the right ascii notation off a whitespace
                               whitespace++;
    }
    printf("well, you typed %d whitespaces",whitespace);
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best Way to Start a Second Program?
    By Adak in forum C Programming
    Replies: 6
    Last Post: 08-13-2009, 01:54 AM
  2. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  3. Start up program
    By Breetai in forum Windows Programming
    Replies: 2
    Last Post: 01-11-2003, 01:12 PM
  4. start another program
    By lshome in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 01:48 PM
  5. Start a program
    By FunkeeMunkee in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:18 PM

Tags for this Thread