C++ Program to Find the Number of Vowels, Consonants, Digits, and White Spaces in a String

#include<iostream>
using namespace std;
int main()
{
char str[150];
int i, vowels, consonants, digits, spaces, symbols;
cout << "\nEnter the string : ";
vowels = consonants = digits = spaces = symbols = 0;
cin.getline(str, 150);
for(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')
{
++vowels;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
++consonants;
}
else if(str[i]>='0′ && str[i]<='9′)
{
++digits;
}
else if (str[i]==' ')
{
++spaces;
}
else
{
++symbols;
}
}
cout << "Vowels: " << vowels << endl;
cout << "\nConsonants: " << consonants << endl;
cout << "\nDigits: " << digits << endl;
cout << "\nWhite spaces: " << spaces << endl;
cout << "\nSymbols: " << symbols << endl;
return 0;
}

OUTPUT:

123 hello world $%&45
Volwels: 3
Consontants: 7
Digits: 5
White spaces: 3
Symbols: 3

Leave a Comment