Table of Contents
C Program to count number of vowels, consonants and spaces in a sentence
In this article, we will discuss the concept of C Program to count number of vowels, consonants and spaces in a sentence
In this post, we are going to learn how to write a program to count the total number of Vowels , consonants and spaces in the given sentence entered by user using loops in C language
Code to count total number of Vowels, consonants and spaces
Code to count number of Vowels, consonants and spaces in given sentence, using for loop – #1
In this code, we are going to learn how to count the total number of Vowels, consonants and spaces in the given sentence 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,spaces=0;//Declare integer variables and initialize to 0
printf("Enter a string for count vowels, consonant and spaces\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
}
else if(str[i] = ' '){
spaces++;//count total number of spaces
}
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
printf("Number of spaces: %d \n",spaces);//display total number of spaces in a string
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a string for count vowels, consonant and spaces C code geeks Number of vowels: 4 Number of consonants: 6 Number of spaces: 3
Code to count number of Vowel, consonant and spaces in given sentence, using while loop – #1
In this code, we are going to learn how to count the total number of Vowels, consonants and spaces in the given sentence using while loop in C language
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];//declare character array
int i,vowCount=0,consCount=0,spaces=0;//Declare integer variables and initialize to 0
printf("Enter a sentence for count vowels, consonant and spaces\n");
//Ask string input for count total number vowels and consonant
gets(str);//reading the input string
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
}
else if(str[i] = ' '){
spaces++;//count total number of spaces
}
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
printf("Number of spaces: %d \n",spaces);//display total number of spaces in a string
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a string for count vowels, consonant and spaces C is a best Language Number of vowels: 7 Number of consonants: 9 Number of spaces: 4
Code to count number of Vowel, consonant and spaces in given sentence, using do-while loop – #3
In this code, we are going to learn how to count the total number of Vowels, consonants and spaces in the given sentence using do-while loop in C language
Program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];//declare character array
int i,vowCount=0,consCount=0,spaces=0;//Declare integer variables and initialize to 0
printf("Enter a sentence for count vowels, consonant and spaces\n");
//Ask string input for count total number vowels and consonant
gets(str);//reading the input string
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 consonant
}
else if(str[i] = ' '){
spaces++;//count total number of spaces
}
i++;
} while(str[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
printf("Number of spaces: %d \n",spaces);//display total number of spaces in a string
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a string for count vowels, consonant and spaces C programming Number of vowels: 3 Number of consonants: 9 Number of spaces: 1
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