•Write a program that asks the user to enter the string using getline function. It then calculates the number of vowels and the number of consonants in the string
–Hint :Remember that spaces or other non alpha characters should be ignored
–Any function that does it?
#include <iostream>
using namespace std;
int main(){
char str[100];
int vowelCounter = 0, consonantCounter = 0;
cout << "Enter any string: ";
cin.getline(str, 150);
//'\0 represent end of string
for(int i = 0; str[i]!='\0'; i++) {
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
vowelCounter++;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
consonantCounter++;
}
}
cout << "Vowels in String: " << vowelCounter << endl;
cout << "Consonants in String: " << consonantCounter << endl;
return 0;
}
OUTPUT:
Enter any string:It is those who concentrate on but one thing at a time who advance in this world.
Vowels in String: 25
Consonants in String: 40
Comments
Leave a comment