Write a program to read a string, then count the vowel in that string. Input: Enter a string: The quick brown fox jumped in the green field. Output: a: 0, e: 6, i: 3, o: , u2:
To count the vowel in a string process the string character by character into switch statement and count the each vowel occurrence in the string.
The required C++ code is:
#include<iostream>
#define SIZE 5
using namespace std;
int main()
{
string str;
int countA=0,countE=0,countI=0,countO=0,countU=0;
cout<<"Enter a string: "; // read string
getline(cin, str);
for(int k=0;k<str.length();k++)
{
switch(str[k]) // process char
{
case 'a': case 'A': // count occurrence of A
countA++;
break;
case 'e': case 'E': // count occurrence of E
countE++;
break;
case 'i': case 'I': // count occurrence of I
countI++;
break;
case 'o': case 'O': // count occurrence of O
countO++;
break;
case 'u': case 'U': // count occurrence of U
countU++;
break;
}
}
cout<<"The frequencey of vowels is: " << endl;
cout<<"a: "<<countA<<", e: "<<countE<<", i: "<<countI<<", o: "<<countO<<", u: "<<countU; // print result
}
Step by step
Solved in 2 steps with 2 images