1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> #include <sstream> #include <string> #include <cctype> namespace Sentence { inline std::string fix (const std::string &s) { std::istringstream istr(s); std::string sentence; sentence.reserve(s.size()); for (std::string word; istr >> word; sentence += word) { if (sentence.empty()) word[0] = std::toupper(word[0]); else sentence += ' '; } if (*sentence.rbegin() != '.') sentence += '.'; return sentence; } }; int main () { std::cout << Sentence::fix(" this is a test of the emergency broadcast system ") << std::endl; std::cout << Sentence::fix(" this is a test of the emergency broadcast system. ") << std::endl; } |