Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Count all the vowels in a given string

C++ String: Exercise-7 with Solution

Write a C++ program to count all the vowels in a given string.

Pictorial Presentation:

C++ Exercises: Count all the vowels in a given string

Sample Solution:

C++ Code :

#include <iostream>

using namespace std;

int Vowel_Count(string text) {
  int ctr = 0;
  for(int i = 0; i < int(text.size()); i++){
    if (text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u')
      ++ctr;
    if (text[i] == 'A' || text[i] == 'E' || text[i] == 'I' || text[i] == 'O' || text[i] == 'U')
      ++ctr;
  }
  return ctr;

}

int main() {
        cout << "Original string: eagerer, number of vowels -> " << Vowel_Count("eagerer") << endl;
        cout << "\nOriginal string: eaglets, number of vowels -> " << Vowel_Count("eaglets") << endl;
        cout << "\nOriginal string: w3resource, number of vowels -> " << Vowel_Count("w3resource") << endl;
        cout << "\nOriginal string: After eagling the Road Hole on Thursday, he missed an 8-footer for birdie Friday., number of vowels -> ";
		cout << Vowel_Count("After eagling the Road Hole on Thursday, he missed an 8-footer for birdie Friday.") << endl;
        return 0;
}

Sample Output:

Original string: eagerer, number of vowels -> 4

Original string: eaglets, number of vowels -> 3

Original string: w3resource, number of vowels -> 4

Flowchart:

Flowchart: Count all the vowels in a given string.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to check whether the characters e and g are separated by exactly 2 places anywhere in a given string at least once.
Next: Write a C++ program to count all the words in a given string.

What is the difficulty level of this exercise?