// Enter your name as a comment for program identification // Program assignment
ID: 3765605 • Letter: #
Question
// Enter your name as a comment for program identification
// Program assignment Vowel.cpp
// Enter your class section, and time
/* The program Vowel.cpp prompts the user to
enter a sentence, counts and displays the
vowels in the sentence. */
/* The input is a sentence.
/* The output is the number of vowels in the
sentence. */
//header files
/* use the correct preprocessor directives
for input/output and string manipulations */
#_________ <iostream>
#_________ <string>
__________ ________________ ___;
/* The function findVowel returns the number
of times a vowel appears in a sentence. */
int findVowel (char letter, int number, string sentence);
// main function
int main()
{
// declare variables
/* declare string variable sentence to
read in a sentence and an integer
to count the number of vowels */
string _______________;
int ______________;
// prompt the user to enter a sentence
____ << "_____________________________ ";
// read in the sentence entered. Use get.line() function
___ . ________(cin, ___________);
// find the length of the sentence and assign to number
________ = s________._________();
// display the length of the sentence
____ << " ____________________ " << ___________ << " ____________ ";
/* call findVowel with each vowel to find the number of times each vowel occurs in the sentence */
____ << " There are " << _________('_', ______, ________) << " a's, "
<< ___________('_', ________, ___________) << " e's, "
<< ___________('_', _______, __________) << " i's, "
<< ___________('_', ______, ___________) << " o's, and "
<< ___________'_', _______, ____________) << " u's ";
return 0;
}
/* The function findVowel returns the number of times a vowel appears in a sentence. */
int findVowel (char letter, int number, string sentence)
{
// declare variables
/* declare string variables newString to process the sentence, integer vowel to count the vowel, start to keep track of the beginning of a substring, stop to keep track of the ending of a substring, and a size_type
variable position to test the return value from the search */
int vowel = 0, start= 0, stop = number;
string newString = sentence;
string::size_type position;
/* begin a loop to search the string for both uppercase and lowercase vowels */
___ (int count = 0; count < 2; count++)
{
// loop until the end of the sentence
_______ (stop)
{
// search for the vowel
position = newString.find(letter);
// if no vowel is found, assign 0 to stop
___ (position == string::npos)
stop = 0;
else
{
/* if a vowel is found assign the location for the start of the next search,increment the vowel counter, calculate the remaining string length search for the vowel */
start = newString.find(letter);
vowel++;
stop = stop - start;
newString = newString.substr(start+1,stop);
}
}
// reinitialize stop to the length of the sentence
stop = number;
// convert the letter to uppercase
letter = toupper(letter);
// reinitialize newString to the sentence
newString = sentence;
}
return vowel;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
/* The function findVowel returns the number
of times a vowel appears in a sentence. */
int findVowel (char letter, int number, string sentence);
// main function
int main()
{
// declare variables
/* declare string variable sentence to
read in a sentence and an integer
to count the number of vowels */
string str;
int length;
// prompt the user to enter a sentence
cout << "Enter a sentence: ";
// read in the sentence entered. Use get.line() function
getline (cin,str);
// find the length of the sentence and assign to number
length = str.length();
// display the length of the sentence
cout << " Length of the string:" << length<< " ";
/* call findVowel with each vowel to find the number of times each vowel occurs in the sentence */
int acnt = findVowel('a', length,str);
int ecnt = findVowel('e', length,str);
int icnt = findVowel('i', length,str);
int ocnt = findVowel('o', length,str);
int ucnt = findVowel('u', length,str);
cout<< " There are " << acnt << " a's, "
<< ecnt << " e's, "
<< icnt << " i's, "
<<ocnt << " o's, and "
<< ucnt << " u's ";
return 0;
}
/* The function findVowel returns the number of times a vowel appears in a sentence. */
int findVowel (char letter, int number, string sentence)
{
// declare variables
/* declare string variables newString to process the sentence, integer vowel to count the vowel, start to keep track of the beginning of a substring, stop to keep track of the ending of a substring, and a size_type
variable position to test the return value from the search */
int vowel = 0, start= 0, stop = number;
string newString = sentence;
string::size_type position;
/* begin a loop to search the string for both uppercase and lowercase vowels */
for(int count = 0; count < 2; count++)
{
// loop until the end of the sentence
start = 0;
while (stop)
{
// search for the vowel
position = newString.find(letter);
// if no vowel is found, assign 0 to stop
if(position == string::npos)
stop = 0;
else
{
/* if a vowel is found assign the location for the start of the next search,increment the vowel counter, calculate the remaining string length search for the vowel */
start = newString.find(letter);
vowel++;
stop = stop - start;
newString = newString.substr(start+1,stop);
}
}
// reinitialize stop to the length of the sentence
stop = number;
// convert the letter to uppercase
letter = toupper(letter);
// reinitialize newString to the sentence
newString = sentence;
}
return vowel;
}
---------------output----------------------
Enter a sentence:
This is test sentence
Length of the string:21
There are 0 a's, 4 e's, 2 i's, 0 o's, and 0 u's
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.