Count vowels in a string in C++
Hello fellow learners! In this tutorial, you will learn about the C++ program to Count Vowels in String. So, let’s learn what are vowels and consonants.
Vowels
The alphabets A, E, I, O, and U are called vowels in English Alphabet. So, overall there are 5 vowels.
Rest all the alphabets are called consonants.
Algorithm to count vowels in a string
- Include string header file.
- Declare the char string input values and i = iteration and vow= count the vowels in a string.
- Take the string from the user.
- Initialize for loop for the given string.
- Initialize the loop for vowels both in both upper case and lower case, entire string will be checked.
- The result will be printed on the output screen as a number.
C++ Code: Count vowels in a string
Read the comments in the code for a better understanding.
#include<iostream> #include<string.h> using namespace std ; int main () { char string []= "CoDeSpEeDy"; int i , vow = 0; for (i = 0; string[i]; i++) { if (string[i]=='a'|| string[i]=='e'|| string[i]=='i'|| string[i]=='o'|| string[i]=='u'|| string[i]=='A'|| string[i]=='E'|| string[i]=='I'|| string[i]=='O'|| string[i]=='U') { vow++;} } cout<<"vowels in the string will be = "<<vow; return 0; }
Run this code online
Output:
vowels in the string will be = 4
Also read: Finding the Number of Vowels, Consonants, Digits, and White Spaces in a String in C++
Leave a Reply