170. C Program to Count Number of Vowels, Consonants and White Spaces in a Given text.
How does this program work?
- This Program is used to count number of vowels, consonants, and white spaces in a given text.
Here is the code
#include <string.h>
int main()
{
char content[200];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", content);
//To check vowels
for(i=0; content[i]!='\0'; ++i)
{
if(content[i]=='a' || content[i]=='e' || content[i]=='i' ||
content[i]=='o' || content[i]=='u' || content[i]=='A' ||
content[i]=='E' || content[i]=='I' || content[i]=='O' ||
content[i]=='U')
{
++vowels;
}
//To check consonants
else if((content[i]>='a'&& content[i]<='z') || (content[i]>='A'&& content[i]<='Z'))
{
++consonants;
}
//To check digits
else if(content[i]>='0' && content[i]<='9')
{
++digits;
}
//To check Numbr of spaces
else if (content[i]==' ')
{
++spaces;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
return 0;
}