Write the function int countVowel(string str) that returns a count of all vowels
ID: 3553371 • Letter: W
Question
Write the function int countVowel(string str) that returns a count of all vowels in the string str.
Vowels are the letters a, e, i, o, and u and their uppercase variants. Strings should be requested from the user
via a loop construct for acquiring test data. HINT: please refer to the following demonstration program for
reading in a string from the user:
// demo of reading in a string from the keyboard
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string input;
while ( true ) {
cout << "Enter a string or Q to quit: ";
getline( cin, input );
if ( input == "Q" )
break;
cout << """ << input << """ << endl; // do something with input
}
return 0;
}
Example run (with user input indicated with bold italics):
Enter a string or Q to quit: The quick brown fox jumped over the lazy dog.
Vowel count: 12
Enter a string or Q to quit: aeiouAEIOU
Vowel count: 10
Enter a string or Q to quit: zxcvbnm
Vowel count: 0
Enter a string or Q to quit: Q
Explanation / Answer
int vowelCount(String input) {
int count = 0;
String vowels = "aeiouAEIOU";
int size = input.length();
for(int i = 0; i < size; i++) {
char c = input.at(i);
int found = vowels.find(c);
if (found != string::npos)
count++;
}
return count;
}
// Call this function to get the vowel count.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.