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?

Here in this c program, we need to count the number of vowels, consonants, and special characters in a sentence. This c program is related to strings. "a, e, i, o, u" are the vowels in the English language. Special characters include space /t, /0 etc. All other than this, we count as consonants.

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, and special characters. To achieve this, we have to check each letter in the given sentence for a vowel or special character or a consonant and increment the respective count.

The main logic of this program is, first set the vowel counter, consonants counter, and special characters counter to zero. 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 a vowel or not by using the 'or' condition. If it is a vowel, then the vowel counter is incremented by 1. Otherwise, the consonants counter is incremented by 1.

Then by using another 'if' condition, we will check whether the character is a special character or not. If it is a special character, then we will increment the special character counter by one. After checking all characters in the sentence, we will decrement the consonants counter for all special characters. Then we will display the number of vowels as vowels counter and a number of consonants as consonants counter.

ALGORITHM

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

STEP 2: Declare the Character Array sentence[100].

STEP 3: Initialize the variable, ConsonantsVowels, Special, and set it to 0.

STEP 4: Accept the Sentence from the user to the variable Sentence using the gets() function.

STEP 5: Using a for loop, take each Character of the String and do step 6.

STEP 6: First, check the Character is a Vowel or not, both in the Upper case and Lower case.

STEP 7: If it is a Vowel, then increment the count of Vowel by 1. That is vowels=vowels+1. Else do step 8.

STEP 8: If the Character is not a Vowel, then increment the consonant counter by 1.

STEP 9: If the Character is a Special character, then increment the Special character count, special by 1. Repeat step 6 until we check all characters of the Sentence.

STEP 10: Decrement the value of the Consonants counter by special characters means c=c-special.

STEP 11: Display the number of Vowels as Vowels counter.

STEP 12: Display the number of consonants as consonants counter.

C Source Code

                                          #include <stdio.h>
#include <string.h>

void main()
{

  char sentence[80];
  int i, vowels = 0, c, special = 0;
  printf("Enter a sentence\n");
  gets(sentence);
  for (i = 0; sentence[i] != '\0'; i++) /*checking for vowels in both lower and upper case */ {
    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; /* increment vowel counter */
    } else {       c = c + 1; /* else increment consonant counter */
    }
    if (sentence[i] == '\t' || sentence[i] == '\0' || sentence[i] == ' ') {
      special = special + 1; /* check and increment special character count  */
    }
  }  c =c - special;
  printf("No. of vowels in %s = %d\n", sentence, vowels); /* display output number of vowels and consonants */
  printf("No. of consonants in %s = %d\n", sentence, c);
}
                                      

OUTPUT

Enter a sentence
Good Morning

No. of vowels in Good Morning = 4
No. of consonants in Good Morning =7