Here is source code of the C program to count the number of vowels & consonants in a sentence. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to read a sentence and count the total number of vowels
* and consonants in the sentence.
int i, vowels = 0, consonants = 0, special = 0;
printf("Enter a sentence \n");
for (i = 0; sentence[i] != '\0'; i++)
if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
'I' || sentence[i] == 'O' || sentence[i] == 'U'))
vowels = vowels + 1;
consonants = consonants + 1;
if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')
special = special + 1;
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
$ cc pgm31.c $ a.out Enter a sentence welcome to sanfoundry No. of vowels in welcome to sanfoundry = 7 No. of consonants in welcome to sanfoundry = 12
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms