Signup/Sign In

C Program To Count The Number of Vowels & Consonants In A Sentence

Learn C language tutorial

Logic:

  • Get the input from the user and store it in the variable,
  • Here, the for loop statement is used to find the vowels in the given string,
  • The condition will check whether the given string has (a,e,i,o,u also the Upper case of these alphabets), if these alphabets are found it will count,
  • If...else condition statement is used to find the consonants in the given string,
  • Other than Vowels all the other alphabets are counted under the consonants,
  • Based on the count it will print the output.

C language Program To Count The Number of Vowels & Consonants In A Sentence:

#include <stdio.h>
 
void main()
{
    char string[50];
    int x, vow = 0, cons = 0, special = 0;
 
    printf("Enter The String To Find The Number Of Vowels And consonants: \n");
    gets(string);
    for (x = 0; string[x] != '\0'; x++)
    {
        if ((string[x] == 'a' || string[x] == 'e' || string[x] ==
        'i' || string[x] == 'o' || string[x] == 'u') ||
        (string[x] == 'A' || string[x] == 'E' || string[x] ==
        'I' || string[x] == 'O' || string[x] == 'U'))
        {
            vow = vow + 1;
        }
        else
        {
            cons = cons + 1;
        }
        if (string[x] =='\t' ||string[x] =='\0' || string[x] ==' ')
        {
            special = special + 1;
        }
    }
    cons = cons - special;
    printf("Numbber Of Vowels In The Given String Is %s = %d\n", string, vow);
    printf("Number Of Consonants In The Given String Is %s = %d\n", string, cons);
}

Output:

Vow Cons