C Program to Count the Number of Vowels and Consonants in a Sentence

This is a C program to count the number of vowels & consonants in a sentence.

Problem Description

This program takes the sentence as input and counts the number of vowels & consonants in a sentence.

Problem Solution

1. Take the sentence as input.
2. Using for loop and if,else statements check for vowels, consonants separately.
3. Print the output accordingly and exit.

Program/Source Code

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.

  1. /*
  2.  * C program to read a sentence and count the total number of vowels
  3.  * and consonants in the sentence.
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     char sentence[80];
  10.     int i, vowels = 0, consonants = 0, special = 0;
  11.  
  12.     printf("Enter a sentence \n");
  13.     gets(sentence);
  14.     for (i = 0; sentence[i] != '\0'; i++)
  15.     {
  16.         if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
  17.         'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
  18.         (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
  19.         'I' || sentence[i] == 'O' || sentence[i] == 'U'))
  20.         {
  21.             vowels = vowels + 1;
  22.         }
  23.         else
  24.         {
  25.             consonants = consonants + 1;
  26.         }
  27.         if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
  28.         {
  29.             special = special + 1;
  30.         }
  31.     }
  32.     consonants = consonants - special;
  33.     printf("No. of vowels in %s = %d\n", sentence, vowels);
  34.     printf("No. of consonants in %s = %d\n", sentence, consonants);
  35. }
Program Explanation

1. Take the sentence as input and store in the array sentence[].
2. Initialize the variables vowels, consonants and special to zero.
3. Using if,else statements, check if the sentence has vowels like a,e,i,o,u,A,E,I,O and U.
4. If it has, then increment the variable vowels by 1. Otherwise increment the variable consonants by 1.
5. If the sentence has \t, \0, & empty space, then increment the variable special by 1.
6. Do steps 3, 4 & 5 inside a for loop.
7. When for loop terminates, subtract the variable consonants from special.
8. Print the variables vowels and consonants as output.

advertisement
advertisement
Runtime Test Cases
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 Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.