C Program to Count Vowels and Consonants in a String

How to write a C Program to Count Vowels and Consonants in a String with an example?.

C Program to Count Vowels and Consonants in a String  Example 1

This program allows the user to enter a string (or character array). Next, it will count how many numbers of Vowel and Consonant present in the user-specified string using If Else Statement.

/* C Program to Count Vowels and Consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	i = vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(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')  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
    	i++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
C Program to Count Vowels and Consonants in a String 1

First, we used While Loop to iterate every character in a String.

while (str[i] != '\0')
{

Next, we used the If Else Statement to check whether each character is equal to a, e, i, o, u, A, E, I, I, O. And if it is TRUE, it is a Vowel otherwise, it’s a Consonant.

You can convert all the characters to the same case using strlwr or strupr. And then, check for lowercase or uppercase elements in the C Programming If condition

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')  {
	vowels++;
}

str[] = Hello World
i = vowels = consonants = 0

While Loop First Iteration: while (str[ i ] != 0)
The condition is True because str[0] = H.

Within the While Loop, we used If Else Statement to check whether it is an Vowel or Consonant.

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’)
=> if(H == ‘a’ || H== ‘e’ || H== ‘i’ || H== ‘o’ || H== ‘u’ || H == ‘A’ || H == ‘E’ || H == ‘I’ || H == ‘O’ || H == ‘U’)

The above condition is false. So, it will execute the statement inside the Else block.
consonants++. It means consonants = 1

Second Iteration: while (str[ 1 ] != 0)
The condition is True because str[1] = e.
if(e == ‘a’ || e == ‘e’ || e == ‘i’ || e == ‘o’ || e == ‘u’ || e == ‘A’ || e == ‘E’ || e == ‘I’ || e == ‘O’ || e == ‘U’)
The above condition is True. So, it will execute the statement inside this block.
vowels++. It means vowels = 1

Do the same for remaining iterations

C Program to Count Vowels and Consonants in a String  Example 2

This program allows the user to enter any string value and count the number of Vowel or Consonant in this string using ASCII Values.

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if(str[i] == 97 || str[i] == 101 || str[i] == 105 || str[i] == 111 || str[i] == 117 ||
		str[i] == 65 || str[i] == 69 || str[i] == 73 || str[i] == 79 || str[i] == 85)  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
 Please Enter any String :  Tutorial Gateway

 Number of Vowels in this String = 7
 Number of Consonants in this String = 9

C Program to Count Vowels and Consonants in a String  Example 3

This C program for vowels and consonants count is the same as the first example, but this time we used the Functions concept to separate the logic.

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>

int check_vowel(char);

int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if(check_vowel(str[i]))  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}

int check_vowel(char c)
{
    if (c >= 'A' && c <= 'Z')
       c = c + 32; 
 
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
       return 1;
 
    return 0;
}
 Please Enter any String :  hello everyone

 Number of Vowels in this String = 6
 Number of Consonants in this String = 8

C Program to Count Vowels and Consonants in a String  Example 4

In this program, we are using the Switch case (alternative to Else If) to count vowels and consonants

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') )  
		{
			switch(str[i])
			{
				case 'a':
				case 'e':
				case 'i':
				case 'o':
				case 'u':
				case 'A':
				case 'E':
				case 'I':	
				case 'O':
				case 'U':
					vowels++;
					break;
				default:
					consonants++;
    		}
    	}
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
 Please Enter any String :  learn c programming

 Number of Vowels in this String = 5
 Number of Consonants in this String = 12

C Program to Count Vowels and Consonants in a String  Example 5

This program to count vowels and consonants is the same as the first example, but this time we are using the concept of the pointers.

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	char *s = str;
  	int vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	while (*s)
  	{
  		if(*s == 'a' || *s == 'e' || *s == 'i' || *s == 'o' || *s == 'u' ||
		*s == 'A' || *s == 'E' || *s == 'I' || *s == 'O' || *s == 'U')  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
    	s++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
 Please Enter any String :  vowels and consonants

 Number of Vowels in this String = 6
 Number of Consonants in this String = 15

Comments are closed.