C Programming Code Examples C > Strings Code Examples Program to count number of vowels and consonants using switch case Program to count number of vowels and consonants using switch case Logic to count number of vowels and consonants Input string from user, store it in some variable say str. Initialize two other variables to store vowel and consonant count. Say vowel = 0 and consonant = 0. Run a loop from start till end of string. Inside the loop increment vowel by 1 if current character is vowel. Otherwise increment consonant by 1 if current character is consonant. /* C program to count total number of vowel or consonant in a string using switch case */ #include <stdio.h> #include <string.h> #define maxsize 100 // Maximum string size int main() { char str[maxsize]; int j, len, vowel, consonant; /* Input strings from user */ printf("Enter any string: "); gets(str); vowel = 0; consonant = 0; len = strlen(str); for(j=0; j<len; j++) { if((str[j]>='a' && str[j]<='z') || (str[j]>='A' && str[j]<='Z')) { switch(str[j]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowel++; break; default: consonant++; } } } printf("Total number of vowel = %d\n", vowel); printf("Total number of consonant = %d\n", consonant); return 0; }