Well rather than passing a string you could just pass a const char*.
1 2 3 4 5 6 7 8
bool hasVowel( constchar* str )
{
if(str == '\0') returnfalse; // base case, end of string... no vowels
elseif( *str == 'a' /*|| ... etc */ ) returntrue; // return true if has vowels...
elsereturn hasVowel( str + 1 );
}
The problem is rather easy to solve, you don't need a recursive function which will make it relatively slower than a while loop, or using an already existing function in std::string.
Not to mention you are making a copy of "blah" each time the function is called which is rather inefficient.
I am using recursion because this is an exercise in a class. I am fairly certain that using the [] is legal, at least my professor said it was. I know that there are much easier ways of doing this, but this is the way I must do it. I am merely asking why it always returns true except when the string is empty. Just as an explanation, the function checks the first letter of the string every time it is called and each time it is called it is passed the previous string minus the first letter. If a vowel is found, true is returned, and if the end is reached and the function is passed an empty string, false is returned.