C program to count Occurrence Of Vowels & Consonants In A String

In this tutorial we are going to learn writing C program to count the Occurrence of vowels and consonants in a given string. Here we will have two variables that will store the count. Using if else we will check Vowel and Consonants. If vowel occurs then increase vowel counter other wise increase consonant counter.

You can visit below program for the basic understandings

C program to check given character is vowel or consonants.

How our vowel and consonant counter program works?

  • Our program will take String as an input.
  • We have two integers which will works as the counter of vowel and consonants.
  • Logic will be like if vowel comes increase the counter of vowel by 1. And if consonant comes then increase the counter of consonant by 1.
  • If character is neither vowel nor consonants then do nothing.

C Program to count vowel and consonant in string

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100];
    int i,j,len=0, vowel =0, consonant= 0;
    printf("C Program to count Vowels and Consonants \n");
    printf("Please enter a string : ");
    fgets(str, 100, stdin);
    len=strlen(str);
    for(i=0; i<len; i++)
    {
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
        {
            vowel++;
        } else if((str[i]>=65 && str[i]<=90)|| (str[i]>=97 && str[i]<=122)) {
            consonant++;
        }
    }
    printf("Total number of vowels are : %d and Consonants are : %d",vowel,consonant);
    return 0;
}

Output 1:

C Program to count Vowels and Consonants 
Please enter a string : Quescol
Total number of vowels are : 3 and Consonants are : 4

Output 2:

C Program to count Vowels and Consonants 
Please enter a string : Education
Total number of vowels are : 5 and Consonants are : 4

[wpusb]