In this article, we will discuss the concept of C++ program to count vowels and consonants in a string
In this post, we are going to learn how to count the vowels and consonants in the given string in C++ programming language
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using for loop in C++ language
Program 1
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { char str[100]; int i,vowCount=0,consCount=0; cout<<"Enter a string for count vowel and consonant\n"; gets(str);//store the input from user 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++;//vowel counter incremented by 1 } else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ consCount++;//consonant counter incremented by 1 } } cout<<"Number of vowels:"<<vowCount<<endl;//display number of vowels cout<<"Number of consonants:"<<consCount<<endl;//display 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 code for coding The number of vowels: 6 The number of consonants: 8
Approach
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using while loop in C++ language
Program 2
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { char str[100]; int i,vowCount=0,consCount=0; cout<<"Enter a string for count vowel and consonant\n"; gets(str); 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++; } else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ consCount++; } i++; } cout<<"The number of vowels:"<<vowCount<<endl; cout<<"The number of consonant:"<<consCount<<endl; getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant CPP programming The number of vowels: 3 The number of consonants: 11
Approach
Program 3
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { char str[100]; int i,vowCount=0,consCount=0; cout<<"Enter a string for count vowel and consonant\n"; gets(str); 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++; } else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ consCount++; } i++; } while(str[i]); cout<<"The number of vowels:"<<vowCount<<endl; cout<<"The number of consonants:"<<consCount<<endl; getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant CPP language The number of vowels: 4 The number of consonants: 7
Approach
Suggested for you
Data type in C++ language
If-else statements in C++ Language
Similar post
C code to check whether the Alphabet is vowel or consonant
Python code to check whether the Alphabet is vowel or consonant
Java code to check whether the Alphabet is vowel or consonant
C++ code to check whether the Alphabet is vowel or consonant
Java program to count vowels and consonants in a string
C program to count vowels and consonants in a string
Python program to count vowels and consonants in a string
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…