Thread: Trying to count vowels in a string assignment

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Question Trying to count vowels in a string assignment

    Could someone look at this code and tell me what I'm doing wrong? I'm sure it's something simple...I just can't stare at it any longer. I'm using a function to count the number of vowels in a string input by the user. When I print the results, I get zero.


    int numvowels (char line[], int length)
    { int b, count = 0;

    for (b = 0; b <= length; b++)
    { if (line[b] == 'A')
    count++;
    if (line[b] == 'E')
    count++;
    if (line[b] == 'I')
    count++;
    if (line[b] == 'O')
    count++;
    if (line[b] == 'U')
    count++;
    if (line[b] == 'Y')
    count++;
    }
    return count;
    }


    Thanks!

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    You have the right idea but you need to test for lower case also. So change it to this:

    Code:
    int numvowels(char line[],int length)
    {    int count = 0;
         for(int b = 0; b < length; b++)
         {    if((line[b] == 'a' || line[b] == 'A') || 
                 (line[b] == 'e' || line[b] == 'E') || 
                 (line[b] == 'i' || line[b] == 'I') || 
                 (line[b] == 'o' || line[b] == 'O') ||
                 (line[b] == 'u' || line[b] == 'U') ||
                 (line[b] == 'y' || line[b] == 'Y'))
               {    count++;
               }
         }
         return(count);
    }
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    Thanks, it works perfectly!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It would be a lot easier (and perhaps more useful) is you had two functions

    isvowel(c)
    Returns true if c is a vowel, false otherwise. See all the other 'is' functions in ctype.h for inspiration.

    numvowels
    Which just calls isvowel in a loop, and counts the number of times it returns true

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    Thanks for your thoughts. Part of the assignment was to only use the function strlen() and do it the hard way. I will definitely keep the other functions handy in the future.

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well for something this simple that is a good idea, but why involve the overhead of function calls if you dont need to? I know this is something simple and it shouldnt matter, but you should never do more than what you are asked for in an assignment (im speaking from experience), some teachers really dont like that and will actually mark you down.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    Think it depends on the lecturer, but if they specifically say to do it a specifc way then ya probly right with that, but most of my lecturer's when I was at uni gave extra marks for doing extra onto the requirements, but that was second year in the subject with asm.

  8. #8
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    While that is true, most teachers dont want you to do more than what you are asked. I had a few prof's that were sticklers for the rules and if you did anything not specified by the assigment implicity or explicity you would be marked down. But then again I had some teachers that were just amazed that you went beyond the requirements. But for safety's sake just do what you are asked and add the more in for your self, just turn in the required work, and expand the problem if you like it. My teacher really didnt like me adding AI into the tic-tac-toe program that we mad for our data structures class so just stick to what your told.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    he probly couldnt work out how it worked and felt stupid so he marked ya down just to show you that hes the one marking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM