Table of Contents
Program to count vowels and consonants in a string in C
In this article, we will discuss the concept of Program to count vowels and consonants in a string in C
In this post, we are going to learn how to write a program to count total number of vowels and consonants in given string input from user in C programming language
Code to find total number of Vowels and consonants in a string
Code to count number of Vowels and consonants in a string, using for loop – #1
In this code, we are going to learn how to count the total number of Vowel and consonant in the given String using for loop in C language
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];//declare character array
int i,vowCount=0,consCount=0;//Declare integer variables
printf("Enter a string for count vowel and consonant\n");
//Ask string input for count total number vowels and consonant
gets(str);//reading the input string
for(i=0; str[i]; 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' ){
vowCount++;//count total number of vowels
}
else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
consCount++;//count total number of consonant
}
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant Java code geeks The number of vowels: 6 The number of consonant: 7
Code to count number of Vowels and consonants in a string, using while loop – #2
In this code, we are going to learn how to count the total number of Vowel and consonant in the given String using while loop in C language
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];//declare array of characters
int i,vowCount=0,consCount=0;//declare variables
printf("Enter a string for count vowel and consonant\n");
//Ask string input from user
gets(str);//Reading string input from user
i=0;
while(str[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' ){
vowCount++;//Count total number of vowels
}
else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
consCount++;//count total number of consonant
}
i++;
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant Java easy code The number of vowels: 6 The number of consonant: 6
Code to count number of Vowels and consonants in a string, using do-while loop – #3
In this code, we are going to learn how to count the total number of Vowel and consonant in the given String using do-while loop in C language
Program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];//Array declaration
int i,vowCount=0,consCount=0;//Variable declaration and initializatuom
printf("Enter a string for count vowel and consonant\n");
//Ask string from the user
gets(str);//reading the input
i=0;
do{
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' ){
vowCount++;//Count total number of vowels
}
else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
consCount++;//count total number of consonants
}
i++;
}while(str[i]);
printf("The number of vowels: %d \n",vowCount);//Display total number of vowels
printf("The number of consonant: %d \n",consCount);//Display total number of consonants
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant my Java code The number of vowels: 4 The number of consonant: 6
Suggested post
Similar post
Java program to print all alphabets in given range
C program to print all alphabets in given range
C++ program to print all alphabets in given range
Java program to print all alphabets using loops
C program to print all alphabets using loops
C++ program to print all alphabets using loops
Java program to check whether a character is Alphabet or Not
C++ program to check whether a character is Alphabet or Not
C program to check whether a character is Alphabet or Not
Python program to check whether a character is Alphabet or Not