In this tutorial you will learn about the C++ Program to Count Vowels in String and its application with practical example.
In this tutorial, we will create a c++ program to count vowels in entered string by the user using c++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators
- Condition statement.
- Array.
- Strings.
- logical operator.
What Is Vowel and Consonant?
Vowel :=> Sound that allowing breath to flow out of mouth, without closing any part of the mouth.
Consonant:=> Sound made by blocking air from flowing out of the mouth with the teeth or throat.
Example
As we know A E I O U are called vowels. And renaming alphabets except these 5 vowels are called consonants.
C++ Program to Count Vowels in String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string.h> using namespace std; int main() { char strng[100]; // declaring variables.. int i,vow=0; cout<<"Enter the string : "; cin>>strng; // taking string from user. //Initializing for loop. for(i=0;strng[i];i++) // iterate loop { //Counting the vowels in a string. if(strng[i]=='a'|| strng[i]=='e'||strng[i]=='i'||strng[i]=='o'||strng[i]=='u'||strng[i]=='A'||strng[i]=='E'||strng[i]=='I'||strng[i]=='O' ||strng[i]=='U') { vow++; } } //Printing number of vowels. cout<<"Total number of vowels in a gievn string are = "<<vow;; return 0; } |
Output
In the above program, we have first declared a variables required in the program.
- strng= hold given string.
- i= for iteration.
- vow= to count vowels in a string.
First of all we will prompted user to enter any string.Then with the help of program we will find the number of vowel in inputted string using following logic.
As user inputs any string we check all vowel with in the string in both lower-case and upper-case as shown in above example using for loop we Initialize for loop and terminate it at the end of string.
As show in above here we will check the entire string with in the loop and count the number of vowels in a string .After the completion of a loop.we will get the number vowel in a following string and print total number vowel present within the string.