Skip to main content

C Program to count total vowel, consonant in sentence

This program will accept input from user. and then by iterating each character from sentence it will count number of vowel and consonant. “A”, “E”,”I”,”O”,”U” either in upper or lower case character are vowels. Other than “A”, “E”,”I”,”O”,”U” all characters between A to Z are consonant.

//Program Name: C Program to count vowel, consonant 

#include<stdio.h>
#include<string.h>
 void main()
 {
   char str[50],ch; //defining variable to accept input
   int chCount=0,cCnt=0,vCount=0;

    printf("\n\tEnter string to count vowel, consonant:\t");
    gets(str); // Read input in "str" variable
    

  while (str[chCount] != '\0')
  {
      ch=str[chCount];
      
   if((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z'))
   {
       if(ch=='a'||ch=='i'||ch=='o'||ch=='u'||ch=='e' ||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
	       vCount=vCount+1;
	    else
	       cCnt=cCnt+1;
   }
    chCount++;
 }

 printf("\nNumber of vowels in given sentence are :%d \t ",vCount);
 printf("\nNumber of Consonants in given sentence are : %d \t ",cCnt);
 }

Output

Enter string to count vowel, consonant: This is software C
Number of vowels in given sentence are :5
Number of Consonants in given sentence are : 10

Program will read sentence in variable str. Every sentence end with ‘\0’ so we are iterating sentence character by character till we not receive ‘\0’. as we need to increment while count manually hence we are using chCount++; Each character is then getting compared with vowels and then associated variable counter is also getting incremented by one.

Click here for another program for vowel and consonants https://ethosspace.com/programmers/c-program-to-check-if-vowel-or-consonant/