Count various types of characters in a string
C program to count the number of vowels, consonants, digits, white-spaces and special characters in a string.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char str[MAX];
int i, upper, lower, vowel, consonant, digit, white_space, special;
printf("Enter the string\t:");
gets(str);
upper = 0;
lower = 0;
vowel = 0;
consonant = 0;
digit = 0;
white_space = 0;
special = 0;
i = 0;
while(str[i] != '\0')
{
if(str[i]>='A' && str[i]<='Z')
upper++;
else if(str[i]>='a' && str[i]<='z')
lower++;
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')
vowel++;
else if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
consonant++;
else if(str[i]>='0' && str[i]<='9')
digit++;
else if(str[i] == ' ')
white_space++;
else
special++;
i++;
}
printf("\nNumber of Uppercase letters\t:%d",upper);
printf("\nNumber of Lowercase letters\t:%d",lower);
printf("\nNumber of Vowels\t:%d",vowel);
printf("\nNumber of Consonants\t:%d",consonant);
printf("\nNumber of Digits\t:%d",digit);
printf("\nNumber of White spaces\t:%d",white_space);
printf("\nNumber of Special Character\t:%d",special);
getch();
}
Output
Enter the string :welcome to COURSECRUX.com@2019
Number of Uppercase letters :10
Number of Lowercase letters :12
Number of Vowels :9
Number of Consonants :13
Number of Digits :4
Number of White spaces :2
Number of Special Character :2