Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the function int countVowel(string str) that returns a count of all vowels

ID: 3556343 • 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:  
  
// getline.cpp - John K. Estell - 07 October 2011
// 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

#include <iostream>
#include <string>

using namespace std;

int countVowels(string str){
int count = 0;
int i;
int len = str.length();
for(i=0;i<len;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++;
}
return count;
}

int main() {
string input;
while ( true ) {
cout << "Enter a string or Q to quit: ";
getline( cin, input );
if ( input == "Q" )
break;
cout << "Vowel Count: " << countVowels(input) << endl;
}
return 0;
}