Write a C++ program that reads in a sentence ending with a period and outputs th
ID: 3675726 • Letter: W
Question
Write a C++ program that reads in a sentence ending with a period and outputs the sentence with spacing corrected and with first letter in the sentence capitalized and the remaining letters non-capitalized. Do not worry about proper names; if their first letters are changed to lowercase that is acceptable. Two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter and should contain no other uppercase letters. Assume that the sentence ends with a period and contains no other periods. You only need to check and convert the fist character of remaining words. For example, the input:
the Answer to life, the Universe, and everything Is 42.
Should produce the following output:
The answer to life, the universe, and everything is 42.
Note: the easiest approach to solve this question is to read your sentence as words not as characters.
You may test your program on the example provided.
In this program you will be using library functions such as touppper, etc.
Explanation / Answer
#include <cctype>
#include <iostream>
string Capitalize(string str)
{
string::iterator it(str.begin());
if (it != str.end())
str[0] = toupper((unsigned char)str[0]);
while(++it != str.end())
{
*it = tolower((unsigned char)*it);
}
return str;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.