find vowels

how to make a program that find vowel in each word.
for ex:
he is good.
no. of vowel:
1
1
2

Last edited on
I don't know what you want to do with it once you've found it, but here I print it.

The pseudo code looks like:
1
2
3
for each line:
    for each character
        if character is a vowel, print it
Are we supposed to input the words, or just read them in a file? If so then see ifstream. You can then store each word in a vector for example then for each word in the vector make a loop in the string to check if the letter is a vowel, and then display it or just increment a variable. Hope this helps.
The code below seems to work well for this, although it could probably be cut back a little. The code takes a string (You must include the string library to use the string variable type) and defines an integer for the current number of vowels in the word, it then runs a loop through the entire sentence and increments currCount each type a vowel occurs. If there is a space or full stop the word has ended so the number of vowels in the word is output and reset. Hope this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string words = "he is good.";
    
    int currCount = 0;
    
    for(int i = 0; i < words.length(); ++i)
    {
        if(words[i] == 'a' || words[i] == 'e' || words[i] == 'i' || words[i] == 'o' || words[i] == 'u')
        {
            ++currCount;
        }
        else if(words[i] == ' ' || words[i] == '.')
        {
            cout << currCount << endl;
            currCount = 0;
        }
    }
Don't forget that sometimes "Y" is a vowel. How can you code for that contigency???

<Cue Evil Laugh...>
Topic archived. No new replies allowed.