Vowels consist of 5 English letters of the alphabet and these are 'a', 'e', 'i', 'o', and 'u'. In this C++ program, we need to count how many vowels are present in a given string and print the count.
Note: We are including both upper case and lower case alphabets for counting vowels.
Below are the following steps to count vowels:
- Declare a count variable and initialize it with 0.
- Run a for loop for the complete length of the given string.
- Check each character for vowel and increment count by one whenever found.
- Print the total count of vowels.
Below is the C++ code Implementation:
//C++ Code to count number of vowels in a string #include<iostream> using namespace std; int main(){ char str[50] = "Algolesson"; //size of string int n = sizeof(str)/sizeof(str[0]); int count = 0; for(int i = 0; i < n; 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'){ count++; } } cout<<"Number of Vowels: "<<count; return 0; }
Number of Vowels: 4
No comments:
Post a Comment