Count the total number of vowels and consonants in a string with C.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, len, vowel, consonant;
/* Input string from user */
printf("Enter any string: ");
gets(str);
vowel = 0;
consonant = 0;
len = strlen(str);
for (i = 0; i < len; i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
/*
* If the current character(str[i]) is a vowel
*/
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
consonant++;
}
}
printf("Total number of vowel = %d\n", vowel);
printf("Total number of consonant = %d\n", consonant);
return 0;
}
Output
Enter any string : Rezaul Karim
Total number of vowel = 5
Total number of consonant = 6
0 Comments