string analysis.

Another question, this time I am pretty much totally stumped. The only way I can think to do this one is with a bunch of really ugly && and ||, there has to be a better way.

Write a function named analyzeString. This function is passed a null terminated string as the first parameter. The function uses 3 reference parameters to return the number of vowels, the number of consonants, and the number of separator characters. Assume a separator character is a space, a tab, or a newline. The function declaration shall be as follows:
void analyzeString (char inputString [], int & numVowels, int & numConsonants, int & numSeparators);


Not necessarily asking anyone to flat out do this for me or anything, just, can someone maybe point me in the right direction?
Well, I would start with defining a function to determine whether or not a character is a vowel. That's easy because there are only 5 vowels, a, e, i, o, and u.

you would have to hardcode that one, but you can then define whether or not a character is a consonant based off of the fact that it isn't a vowel.

1
2
3
4
5
6
bool isConsonant(char c)
{
    return !isVowel(c);
    //EDIT: You probably also want to check that it's an alphabetical character as well.
    //see std::isalpha (http://www.cplusplus.com/reference/cctype/isalpha/)
}


then you could define a isSeparator function that returns true if a character is ' ', '\t', or '\n'.

After you have those it's just a matter of looping through inputString and running the 3 tests on each character, and incrementing the outputs accordingly.
Last edited on
Thank you!
Topic archived. No new replies allowed.