C Program to count vowels & consonants of a sentence

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What are vowels, consonants, and special characters in a string?

The English alphabet consists of 26 letters where 5 of them are called vowels and the rest of the 21 letters are called consonants. "a, e, i, o, u" are the vowels in the English language, all other than this are consonants.

This C program counts the number of vowels and consonants and it is related to strings and characters. We will check if a character matches with a,e, i,o,u then count it as a vowel, and all others as consonants. Special characters include space,tab,newline etc.

How to count the vowels, consonants, and special characters in C?

In this C program, we need to calculate the count of each of the vowels, consonants. To achieve this, we have to check each letter in the given sentence and increment the respective count for a vowel or a consonant.

The main logic of this program is, first set the counters for vowel and consonant to zero. Read a sentence from the user and take each character of the string using for loop until it reaches the end of the sentence, a null character. Then check each character of the sentence is whether it's a vowel or not by using the if condition. If it is a vowel, then increments the vowel counter otherwise, the consonant counter is incremented.

After checking all characters in the sentence, we will display the number of vowels in the vowels counter and the number of consonants in the consonants counter.


STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare character arrays sentence[100]vow[ ]="aeiou"

STEP 3: Initialize the counter variables v_count c_count and set it to 0.

STEP 4: Accept the sentence from the user to the variable sentence using the fgets() function.

STEP 5: Using a for loop, take each character of the string and do step 6 until the last character

STEP 6: First, check if the character is not a Special character then do step 7 else go for the next iteration of the loop.

STEP 7: Using strchr() function checks if the character is present in vowels array vow[ ], both upper and lower cases are checked here converting the character by tolower() function. If it is a Vowel, then increment v_count count by 1 else increments the c_count count by 1.

STEP 8: Display the number of vowels in the v_count counter and the number of consonants in the c_count counter.

C Source Code

                                          #include <stdio.h>

#include <string.h>


void main() {

  char sentence[100] , vow[] = "aeiou" ;
  int i , v_count = 0 , c_count = 0 ;
  char * st ;

  printf ("Enter a sentence\n") ;
  fgets (sentence, 100, stdin) ;
  for (i = 0; sentence[i] != '\0'; i++) {
    if (sentence[i] != '\t' && sentence[i] != ' ' && sentence[i] != '\n') {
      st = strchr ( vow, tolower(sentence[i]) ) ;  /*checking for vowels in both lower and upper case */
      if (st != NULL)
        v_count = v_count + 1 ;  /* increment vowel counter */
        c_count = c_count + 1 ;   /* increment consonant counter */
  /* display output number of vowels and consonants */
  printf("\nNo. of vowels is %d", v_count );
  printf("\nNo. of consonants is %d ", c_count );


Enter a sentence
Good Morning

No. of vowels is 4
No. of consonants is 7