Program to find the vowels in given string
Written by
Check vowels
In this example, we’ll find the vowels in the string provided by the users.
Logic: Here we’ll take an array of size 5 such that, its each index stores the count of a vowel. Then, we search the vowels from the string and increment the value at the particular position of the array.
Algorithm:
- Take a string as input.
- Declare an array of size 5.
- Traverse the string and print the count array.
Code:
#include <iostream>
#include <string>
using namespace std;
int vowel(char c)
{
switch(c)
{
case 'a' :
{return 0;break;}
case 'A' :
{return 0;break;}
case 'e':
{return 1;break;}
case 'E':
{return 1;break;}
case 'i' :
{return 2;break;}
case 'I':
{return 2;break;}
case 'o':
{return 3;break;}
case 'O':
{return 3;break;}
case 'u' :
{return 4;break;}
case'U':
{return 4;break;}
default:
{return 5;break;}
}
}int main()
{
string str;
int count[5]={0},x;
cout<<"Enter a string: ";
getline(cin,str);
int len=str.length();
for(int i = 0; i<len; i++)
{ x=vowel(str[i]);
if(x<5)
count[x]+=1;
}
cout<<"\n a:"<< count[0];
cout<<"\n e:"<<count[1];
cout<<"\n i:"<<count[2];
cout<<"\n o:"<<count[3];
cout<<"\n u:"<<count[4];
return 0;
}
Output:
Enter a string: aeiouaeiou
a:2
e:2
i:2
o:2
u:2