Skip to content
Home » C program to count number of vowels and consonants in a given sentence

C program to count number of vowels and consonants in a given sentence

Below is the code to count number of vowels and consonants in a given sentence

#include <stdio.h>
int main(){
    char str[1000];
    printf("Enter a sentence: ");
    fgets(str,1000,stdin);
    int i=0,vowels_count=0,cons_count=0;
    for(i=0;str[i]!='\0';i++){
        if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U'){
            vowels_count++;
        }
        else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) 
        {

            cons_count++;
        }
    }
    printf("Vowels count in the above sentence is %d\n",vowels_count);
    printf("Consonants count in the above sentence is %d",cons_count);
}

Output:

Enter a sentence: similar geeks
Vowels count in the above sentence is 5
Consonants count in the above sentence is 7