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 learn to count vowels in entered string by the user using c++ programming.
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Condition statement.
- 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.
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.
using namespace std;
char strng; // declaring variables..
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')
//Printing number of vowels.
cout<<"Total number of vowels in a gievn string are = "<<vow;;
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.