C Program to count the number of vowels and consonants in a string


In this program, You will learn how to count the number of vowels and consonants in a string in c.


Learn Java

Total vowels are: 4

Total consonants are: 5

Example: How to count the number of vowels and consonants in a string in c.

#include<stdio.h>

int main() {

   char str[100];
   int i = 0, vols = 0, cons = 0;

   printf("Enter any string:");
   scanf("%[^\n]%*c", str); 

   while (str[i] != '\0') {
       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') {
           vols++;
       } else if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= 'a' && str[i] <= 'z') {
           cons++;
       }
       i++;
   }
   printf("Total vowels is:%d", vols);
   printf("\nTotal consonants is:%d", cons);

   return 0;
}

Output:

Enter any string:Xiith.com
Total vowels is:3
Total consonants is:5