944,135 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2244
  • C++ RSS
Nov 28th, 2006
0

Need help with counting " Y" as a vowel

Expand Post »
I am trying to get this code working for extra credit, but I unfortunately only have 40 minutes until it is due:rolleyes:. I would like to know how to do this program, whether I turn it in or not. The letter "Y" can be considered a vowel if there is not a vowel immediately before or immediately after the "y", otherwise it should be considered a consonant. I have everything working except for the Y being counted as a vowel when it is next to consonants.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cctype>
  3. using namespace std;
  4.  
  5. int Y(char string[], int ctr, int vowel);
  6. int length = 0;
  7. int vowel = 0;
  8.  
  9. int main()
  10. {
  11. char string[50] = "";
  12. cout << "Please enter a string of 50 characters or less: ";
  13. cin.getline(string, 50);
  14. length = strlen(string);
  15.  
  16. for(int ctr = 0; ctr < length; ctr++)
  17. {
  18. string[ctr] = tolower(string[ctr]);
  19.  
  20.  
  21. if(string[ctr] == 'a' || string[ctr] == 'e' || string[ctr] == 'i' || string[ctr] == 'o' || string[ctr] == 'u')
  22. {
  23. vowel++;
  24.  
  25. if(string[ctr] == 'y')
  26. {
  27. vowel = Y(string, ctr, vowel);
  28. }
  29. }
  30.  
  31. if(string[ctr] == ' ')
  32. {
  33. vowel++;
  34. }
  35.  
  36. }
  37. cout << "\nThe string you entered was: "<< string << endl;
  38. cout << "You entered " << (length - vowel) << " consonants in that string." << endl;
  39. }
  40.  
  41. int Y(char string[], int ctr, int vowel)
  42. {
  43. bool flag1 = false;
  44. bool flag2 = false;
  45. if(string[ctr++] == 'a' || string[ctr++] == 'e' || string[ctr++] == 'i' || string[ctr++] == 'o' || string[ctr++] == 'u')
  46. {
  47. flag1 = true;
  48. }
  49. if(string[ctr--] == 'a' || string[ctr--] == 'e' || string[ctr--] == 'i' || string[ctr--] == 'o' || string[ctr--] == 'u')
  50. {
  51. flag2 = true;
  52. }
  53. if((flag1 == true) && (flag2 == true))
  54. {
  55. vowel--;
  56. }
  57. return vowel;
  58. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newbie101 is offline Offline
2 posts
since Nov 2006
Nov 28th, 2006
0

Re: Need help with counting " Y" as a vowel

>>but I unfortunately only have 40 minutes until it is due
unfortunately for you the time expired 6 hours ago. Lesson learned: don't wait until the last minute to get help.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 28th, 2006
0

Re: Need help with counting " Y" as a vowel

Well, I had a major project that was due as well as finals all this week, so I did not do the extra credit until everything else was finished. Anyway, I would still like to know how to do it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newbie101 is offline Offline
2 posts
since Nov 2006
Nov 28th, 2006
0

Re: Need help with counting " Y" as a vowel

here is something just off the top of my head -- untested and uncompiled.
C++ Syntax (Toggle Plain Text)
  1.  
  2. int isvowel(char c)
  3. {
  4. if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
  5. return TRUE;
  6. return FALSE;
  7. }
  8.  
  9. ...
  10. ...
  11. if( string[ctr] == 'y' )
  12. {
  13. if( !isvowel(strin[ctr-1]) && !isvowel(strin[ctr+1])
  14. {
  15. // y is a vowel
  16. }
  17. }

your program also needs to verify that (ctr-1) >= 0 && (ctr+1) <= length of the string.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dll's and problems with them
Next Thread in C++ Forum Timeline: help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC