Write a program that prompts the user to input a sequence of characters and outp
ID: 3674430 • Letter: W
Question
Write a program that prompts the user to input a sequence of characters and outputs A, E, I, O, U, and Y and the number of times each vowel appears in the input string.
A null-return function, inString, that prompts the user to input a sequence of characters.
A value-returning function, isVowel, that returns the value true if a given character is a vowel and otherwise returns false.
A null-return function, outVowels, that outputs a list of each vowel and the number of times that vowel appeared in the input sequance.
Write a program that prompts the user to input a sequence of characters and outputs A, E, I, O, U, and Y and the number of times each vowel appears in the input string.
The program should include 3 functions:A null-return function, inString, that prompts the user to input a sequence of characters.
A value-returning function, isVowel, that returns the value true if a given character is a vowel and otherwise returns false.
A null-return function, outVowels, that outputs a list of each vowel and the number of times that vowel appeared in the input sequance.
Explanation / Answer
#include <iostream>
#include<string.h>
#include <ctype.h>
using namespace std;
void inString(char *str){
cout<<"Enter a string: ";
cin.getline(str, 50);
}
bool isVowel(char c){
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U'||c=='y'||c=='Y') {
return true;
}
else {
return false;
}
}
void outVowels(int out[]){
cout<<"Count for A: "<<out[0]<<endl;
cout<<"Count for E: "<<out[1]<<endl;
cout<<"Count for I: "<<out[2]<<endl;
cout<<"Count for O: "<<out[3]<<endl;
cout<<"Count for U: "<<out[4]<<endl;
cout<<"Count for Y: "<<out[5]<<endl;
}
int main() {
char str[50]; // input array
//taking user input
inString(str);
int length = strlen(str);
int countVowel[6] = {0}; // vowel counter, A, E, I, O, U, Y
for(int i=0; i<length; i++){
if(isVowel(str[i])){
char c = tolower(str[i]); // coverting into lower case
// based on vowel, incrementing counter
switch(c){
case 'a' :
countVowel[0]++;
break;
case 'e' :
countVowel[1]++;
break;
case 'i' :
countVowel[2]++;
break;
case 'o' :
countVowel[3]++;
break;
case 'u' :
countVowel[4]++;
break;
case 'y' :
countVowel[5]++;
}
}
}
//printing vowel count
outVowels(countVowel);
return 0;
}
/*
output:
Enter a string: adadweewwdiwod wsxcscuwmfswy eedjwe
Count for A: 2
Count for E: 5
Count for I: 1
Count for O: 1
Count for U: 1
Count for Y: 1
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.