C program to count vowels and consonants in string

C-String-Programs C-Programs

In this article, we will learn about how to count the number of vowels and consonants present in a string in the C language.

We will be iterating over the string with the help of a while loop and in each iteration, we will be checking to validate the presence of a vowel. To validate this we will make use of the if statement and logical || (OR operator) by setting up conditions for all vowels. For the iterations where this validation will fail, we will count that character as a consonant.
This will give us counts of both vowels and consonants in the given string.

C program to count vowels and consonants in string

#include <stdio.h>

int main() 
{
   char str[] = "DevEnum Tutorials";     
   int i = 0;
   int vowels = 0;            
   int consonants = 0;        

   while(str[i++] != '
#include <stdio.h>

int main() 
{
   char str[] = "DevEnum Tutorials";     
   int i = 0;
   int vowels = 0;            
   int consonants = 0;        

   while(str[i++] != '\0') 
   {
      if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' )
         vowels++;
      else
         consonants++;
   }

   printf("The Given string '%s' contains %d vowels and %d consonants.", str, vowels, consonants);

   return 0;
}
') { if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ) vowels++; else consonants++; } printf("The Given string '%s' contains %d vowels and %d consonants.", str, vowels, consonants); return 0; }

Output

The Given string 'DevEnum Tutorials' contains 6 vowels and 11 consonants.