write in c++ Do not prompt the user for input during the execution of your progr
ID: 3695791 • Letter: W
Question
write in c++
Do not prompt the user for input during the execution of your program. Write a program named Lab3.cpp. The program should satisfy the requirements specified in chapter 10, program challenge #5, Sentence Capitalizer with the following exceptions: instead of asking the user to input a string and passing it to the function, initialize the C-string with the following string: "no, not tonight. it's a very popular place and you have to make reservations in advance. besides, it's expensive, and I don't have any money." The modified string should look like: "No, not tonight. It's a very popular place and you have to make reservations in advance. Besides, it's expensive, and I don't have any money." The punctuation marks that signal the end of a sentence are the period, the question mark and the exclamation mark. So your code should work with sentences that end with the question mark and the exclamation mark too. Yes, your program needs to include the main function that prepares the C-string and calls the function as written there in the book. Display the text before and after the call. The output of your program should look something like the following: The text before the modification: no, not tonight. it's a very popular place and you have to make reservations in advance. besides, it's expensive, and I don't have any money. The text after the modification: No, not tonight. It's a very popular place and you have to make reservations in advance. Besides, it's expensive, and I don't have any money.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
void sentenceCase(string&);
void lowerCase(string&);
bool isSentencePunc(char);
int main()
{
string strToConvert = "no, not tonight. it's a very popular place and you have to make reservations in advance."
"besides, it's expensive, and I don't have any money."; //The string that will be converted
sentenceCase(strToConvert);
cout << "New String: " << strToConvert;
return 0;
}
/****** function sentenceCase *****
Description: Converts a string so that the first letter of the first word
of a sentence is capitalized.
PARAMETERS
strToConvert: the string being manipulated
Precondition:
strToConvert: undefined
HEADERS
#include <string>
Postcondition:
Returns the manipulated string by reference */
void sentenceCase(string& strToConvert)
{
//Identifies if the sentence has been capitalized. Set to false by default.
bool thisSentenceCapped = false;
lowerCase(strToConvert); //Lowercase the string before processing.
for (unsigned int i=0; i<strToConvert.length();i++)
{
//At a punctuation mark, the next sentence has not been manipulated
//yet to have its first letter capitalized, so thisSentenceCapped is false.
if (isSentencePunc(strToConvert[i]))
thisSentenceCapped = false;
if ((thisSentenceCapped==false) && (isalpha(strToConvert[i])))
{
strToConvert[i]=toupper(strToConvert[i]);
thisSentenceCapped = true;
}
}
}
/****** function lowerCase *****
Description: makes all the characters of a string lowercase
PARAMETERS
strToConvert: the string being manipulated
Precondition:
strToConvert: undefined
HEADERS
#include <string>
Postcondition:
Returns the string all lowercase */
void lowerCase(string& strToConvert)
{
for(unsigned int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = tolower(strToConvert[i]);
}
}
/****** function isSentencePunc *****
Description: Checks to see if a character is a punctuation mark used to denote
the end of a sentence. (! . ?)
PARAMETERS
character: The character being tested
Precondition:
character: defined
Postcondition:
Returns boolean value of true if the character is ! . or ? */
bool isSentencePunc(char character)
{
switch(character)
{
case '!':
case '.':
case '?':
return true;
default:
return false;
}
}
/*
output:
New String: No, not tonight. It's a very popular place and you have to make reservations in advance.Besides, it's expensive, and i don't have any money.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.