Vowels, Consonants, Digits and Spaces in a String
- Anonymous
- Aug 5, 2015
- 1 min read
Q. Design a program to count the vowels, consonants, digits and spaces in a given string.
SAMPLE INPUT :
Hello World. I am XYZ and I am 19 years old.
SAMPLE OUPUT :
Vowels = 11
Consonants = 19
Digits = 2
Spaces = 10
PRE-REQUISITES : - Basic knowledge of Strings
- Receiving Input using cin.getline function
- Loops
CODE :
#include <iostream>
using namespace std;
int main()
{
int i, d=0, c=0, v=0, space=0;
char str[50];
cout<<"Enter String : ";
cin.getline(str, 50, '\n');
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')
{
v++;
}
else if(str[i]>='a' && str[i]<='z' || str[i]>='A' && str[i]<='Z')
{
c++;
}
else if(str[i]>='0' && str[i]<='9')
{
d++;
}
else if(str[i]==' ')
{
space++;
}
}
cout<<"\nVowels = "<<v<<"\nConsonants = "<<c<<"\nDigits = "<<d<<"\nSpaces = "<<space<<"\n";
}
Comments