Read the question, write C++ on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read one line and print it in title case: first letter of each word upper, rest lower.
Input. One line of words.
Output. Title-cased line.
Examples
Input
hello world
Output
Hello World
Input
PYTHON labs
Output
Python Labs
Hint
- Track whether the previous character was a space.
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string line;
std::getline(std::cin, line);
bool start = true;
for (char &ch : line) {
if (std::isspace((unsigned char)ch)) { start = true; }
else if (start) { ch = (char)std::toupper((unsigned char)ch); start = false; }
else { ch = (char)std::tolower((unsigned char)ch); }
}
std::cout << line << "\n";
return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.