Counting Vowels within a string.

This is a discussion on Counting Vowels within a string. within the C Programming forums, part of the General Programming Boards category; Hi, I have created a program counting vowels in a string Vowels (A, E, I, O, U, a, e, i, ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Question Counting Vowels within a string.

    Hi, I have created a program counting vowels in a string

    Vowels (A, E, I, O, U, a, e, i, o, u).

    I don't know how to get it to show something like this:

    Input: This is a string.

    Output: The number of Vowels is: 4

    Code:
    #include <stdio.h>
    #define SIZE 50
    
    int main (void)
    {
    
    int va = 0;              /*Vowel counters*/
    int ve = 0;
    int vi = 0;
    int vo = 0;
    int vu = 0;
    
    char c;                   /*Character input to be placed into array*/
    char s[SIZE];         /*Character Array*/
    int i=0;
    
    puts("Enter a Line of Text:");
    
                          
    while ((c = getchar()) != '\n'){
    s[i++] = c;
    }
                            
                           
        
    
    puts ("\n");
    puts(s);
    printf("\n");
    printf("%d", a);
    printf("\n");
    
    return 0;
    }
    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    You can check each character at a time. So in your while loop you can have
    Code:
    if(c=='a' || c=='A')va++;
    and so on for the other vowels.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Talking Excellent, thank you very much.

    This is perfect, thank you very much. That is exactly what I needed.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Note that this
    Code:
    if(c=='a' || c=='A')va++;
    is the same as this
    Code:
    if(tolower(c) == 'a') va++;
    if <ctype.h> is #included. It's a bit easier to use. (There's a toupper() as well.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    Code:
    #include <stdio.h>
    #define SIZE 50
    
    
    void CountVowelLetters(char, int, int, int, int, int);
    void CountVowelLetters1(int, int, int, int, int);
    
    int main (void)
    {
    int vowels;
    int va = 0;
    int ve = 0;
    int vi = 0;
    int vo = 0;
    int vu = 0;
    char c;
    char s[SIZE];
    int i=0;
    
    puts("Enter a Line of Text:");
    
    while ((c = getchar()) != '\n'){
    s[i++] = c;
    CountVowelLetters(c, va, ve, vi, vo, vu);
    }
    
    puts("The number of Vowels A,E,I,O,U,a,e,i,o,u is:");
    
    return 0;
    }
    
    void CountVowelLetters(char c, int va, int ve, int vi, int vo, int vu){
    int vowel;
    
    if(c=='a' || c=='A')va++;
    if(c=='e' || c=='E')ve++;
    if(c=='i' || c=='I')vi++;
    if(c=='o' || c=='O')vo++;
    if(c=='u' || c=='U')vu++;
    
    vowel = (va + ve + vi + vo + vu);
    printf("%d", vowel);
    
    }
    Have it all set up, but I don't understand how to do this:

    Input:
    This is ok

    Output:
    The number of Vowels A,E,I,O,U,a,e,i,o,u is: 3

    Instead i have
    Output:
    This is ok
    0010010010The number of Vowels A,E,I,O,U,a,e,i,o,u is:

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The trouble is that changes to your variables in CountVowelLetters() are not reflected in main(), so you don't have an accumulating count.

    The easiest way to solve this is to use pointers as parameters to CountVowelLetters().
    Code:
    CountVowelLetters(c, &va, &ve, &vi, &vo, &vu);
    /* ... */
    void CountVowelLetters(char c, int *va, int *ve, int *vi, int *vo, int *vu){
    /* ... */
    if(c=='a' || c=='A') (*va)++;
    /* etc */
    The parentheses around (*va) are necessary because ++ has a higher precedence than va. For example, just plain
    Code:
    *va++;
    is like
    Code:
    *(va ++);
    which does not do what you want.

    If you don't know about pointers, I've probably just confused you. Perhaps you should look at this pointer tutorial. http://www.cprogramming.com/tutorial/c/lesson6.html

    Another solution is to put everything into main() and do away with CountVowelLetters() -- that way you don't have to use pointers. But I'm guessing you're supposed to have this function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by dwks View Post
    The parentheses around (*va) are necessary because ++ has a higher precedence than va. For example, just plain
    Code:
    *va++;
    is like
    Code:
    *(va ++);
    which does not do what you want.
    Or, you could just write ++*va, which is unambiguous because both unary operators are binding on the same side, therefore precedence doesn't even enter the equation.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Talking Much Better

    I thnk i understand what you guys are saying, if i put it into a pointer i can call it back later in the program. I would like to thank you guys for such quick responses. This is a much better understanding then my text.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by brewbuck View Post
    Or, you could just write ++*va, which is unambiguous because both unary operators are binding on the same side, therefore precedence doesn't even enter the equation.
    That's true. Sigh. All those extra parentheses I could have saved, had I thought of this myself.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    Ok this is what I think it is supposed to be but not working still.

    Code:
    #include <stdio.h>
    #define SIZE 50
    
    
    void CountVowelLetters(char, int *, int *, int *, int *, int *, int *);
    
    int main (void)
    {
    int *vPtr; 
    int *aPtr;
    int *ePtr; 
    int *iPtr; 
    int *oPtr;
    int *uPtr;
    
    int vowel = 0;
    int va = 0;
    int ve = 0;
    int vi = 0;
    int vo = 0;
    int vu = 0;
     
    char c;
    char s[SIZE];
    int i=0;
    
    aPtr = &va;
    ePtr = &ve;
    iPtr= &vi;
    oPtr = &vo;
    uPtr = &vu;
    vPtr = &vowel;
    
    puts("Enter a Line of Text:");
    
    while ((c = getchar()) != '\n'){
    s[i++] = c;
    
    CountVowelLetters(c, &va, &ve, &vi, &vo, &vu, &vowel);
    
    }
    
    puts("The number of Vowels A,E,I,O,U,a,e,i,o,u is:");
    printf("&#37;d", vPtr );
    
    
    return 0;  
    }
    
    void CountVowelLetters(char c, int *va, int *ve, int *vi, int *vo, int *vu, int *vowel){
    
    if(c=='a' || c=='A')(*va)++;
    if(c=='e' || c=='E')(*ve)++;
    if(c=='i' || c=='I')(*vi)++;
    if(c=='o' || c=='O')(*vo)++;
    if(c=='u' || c=='U')(*vu)++;
     
    *vowel = (*va + *ve + *vi + *vo + *vu);
    }
    I gives me a -4197080 number every time.

    I am so close...........please inform what the heck I have done wrong here.

  11. #11
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    printf("&#37;d", *vPtr );

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    or
    printf("&#37;d", vowel);

    and you better count number of chars stoored in the string to avoid memory overruns

    proper indentation never hurts.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    I guess that would make sense.

    i took out

    Code:
    int *vPtr; 
    int *aPtr;
    int *ePtr; 
    int *iPtr; 
    int *oPtr;
    int *uPtr;
    
    aPtr = &va;
    ePtr = &ve;
    iPtr= &vi;
    oPtr = &vo;
    uPtr = &vu;
    vPtr = &vowel;
    Thanks guys, you are making this very easy to understand. It works properly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21