Vowel counting

I have to write a program that prompts the user to input a sequence of characters and outputs the number of vowels. The previous problem had me return true or false if inputted letter is a vowel or not so I have that.

I have looked at a bunch of other posts but they all include string.length, which I have not learned (therefore cannot use) and arrays (same thing).

This is what I have. I thought something along the lines of cin.get(ch) but can't get that working either.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

bool isvowel(char x);

int main()
{
    string phrase;

    cout << "Enter a series of characters: ";
    cin >> phrase;



   system("pause");
}

bool isvowel(char x)
{
   if ((x == 'a') || (x == 'A') || (x == 'e') || (x == 'E') || (x == 'i')
   || (x == 'I') || (x == 'o') || (x == 'O') || (x == 'u') || (x == 'U'))
   	return 1;
   else
	return 0;
}
Last edited on
string.length() will just return the number of letters in your word.
So you can use a simple for loop for this.

1
2
3
4
5
6
7
8
9
10
11
12
bool x;
int counter=0;
for (int i = 0; i < phrase.length(); i++)
{
 x = isvowel(phrase[i]); 
 if (x == true)
{
counter++;
}

}
cout << " There are " << counter << " vowels in " << phrase << endl;


The square bracket with i just gets each letter of your phrase and checks with bool isvowel.
so if you phrase is apple. Then it will take 'a' and check it if it is vowel or not. if it is vowel, which means if bool function returns true, counter gets a +1. It does this (loops) until it checks the last character of phrase.

Hope this helps!
Last edited on
Thank you so much for the response. This works! I haven't come across .length or [] before (i think) so I was afraid to use it. Appreciate the help!

Topic archived. No new replies allowed.