-
clegs I gave you the recursive sol'n
The base case involves the empty string, which contains zero vowels. For other cases, examine the first letter and add one, if necessary, to the result of applying the procedure to the substring consisting of all letters except the first.
Code:
if (string[0] == "")
return 0;
else
//finish it...
-
Mister C:
I tried your statment for the base case:
Code:
if (phrase[0] == "")
return 0;
I receive two errors while compiling!
c:\Documents and Settings\...\T3Q24_Vowels.cpp(29): error C2040: '==' : 'int' differs in levels of indirection from 'const char [1]'
c:\Documents and Settings\...\T3Q24_Vowels.cpp(29): error C2446: '==' : no conversion from 'const char *' to 'int'
so, I reverted back to the phrase.empty() becasue it not only doesn't have vowels it doesn't have anything. phrase.empty() works as a base case, but the rest doesn't.
However I changed the string to a pass by reference and changed the OR list of vowel checks to case statement and I now get just one vowel no matter what the string contains. I also changed the return to use the phrase.substr() to knock off the first character in the string, or that was my intent.
the new code is:
Code:
int vowels(const string& phrase)
{
if (phrase.empty())
return 0;
else
switch(phrase[0])
{
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
return vowels(phrase) + vowels(phrase.substr(1, phrase.size()));
}
}
I appreciate the help and am trying all the suggestions.
Thanks!
-
Here is one way to do it in Java:
Code:
int countVowels(String s) {
if (s.length() == 0) return 0;
int tailResult = countVowels(s.substring(1));
switch (s.charAt(0)) {
case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’:
return tailResult+1;
default:
return tailResult;
}
}
-
Thanks!
I have copied that into my list of possibilities I have been collecting.
I understand that code so I will try to translate that into C++.
I really do appreciate all the help, really!
I have been at this code for 15 hours!
I was able to get the palindrome code to work about 1 1/2 hours ago, so that was a success.
And, now this one is also!!!
After 15 1/2 hours I have a functioning code.
The palindrome was a much easier function to understand than the vowel counting, because counting the vowels was the hang up for me.
Thank you Mister C, laserlight, and MacGyver. After looking at the JAVA version and putting it together with the palindrome working code and taking the suggestions from you all, I am finished, YAHOO!
-
-
MacGyver, Thanks a bunch!
I am taking a bow, and actually starting my reading for the next chapter - Stacks!
I have a good three hours left for studying, and, I know nothing about them at this point!
Thank you all again!
Cindy
Oh, yea! I hope I don't see recursion again in the languages I have coming up. I have three more, so there is a good chance I will. UGH! Maybe by then the light will have at least flickered a little.
-
I know sleep is overrated, but you should consider taking at least a small break occasionally. Otherwise, your chances of making mistakes increases, and you really don't want that to happen. ;)
Stacks are simple. They are a collection of items, but you are only able to mainly push an item onto the stack or pop the last item off. Some stacks get a little more complicated than that, but that is the main idea.
Back to recursion, you will most likely encounter it a lot, especially when you get into more advanced data types, such as trees. The trick is to practice it a lot until you're used to it. Think of almost any problem that requires a loop to solve, and think about how you can solve such problems recursively.
-
Sleep, that is close to a four-letter word for me these days!
Glad to hear stacks are simple, I deal well with simple. We are alike, stacks and me. HA!
And, about that recursion statement! Thanks! You might as well have hit me with that deathray of yours.
Keep it handy, K!
Thanks again, I feel much less stressed now that I have been able to move on to the next chapter.
This board has kept me ?sane?. Until this class is over, or I have flunked for not completing it on time, I'll Be Back!