Problem
Convert text to title case: the first letter of every word becomes uppercase and the remaining letters lowercase. Words are separated by single spaces.
Implement title_case(text).
Input
text: Lowercase or mixed-case words separated by single spaces.
Output
Return the title-cased text.
Constraints
0 ≤ length ≤ 10,000 (letters and spaces)
Examples
Input:
text = "hello world"
Expected output:
expected = "Hello World"
Input:
std::string text = "hello world";
Expected output:
std::string expected = "Hello World";
Input:
string text = "hello world";
Expected output:
string expected = "Hello World";
Explanation
"hello" → "Hello" and "world" → "World".
Input:
text = "tHE qUICK fox"
Expected output:
expected = "The Quick Fox"
Input:
std::string text = "tHE qUICK fox";
Expected output:
std::string expected = "The Quick Fox";
Input:
string text = "tHE qUICK fox";
Expected output:
string expected = "The Quick Fox";
Explanation
Remaining letters become lowercase: "The Quick Fox".
How to approach it
Try on your own first, then open one hint at a time.
Hint 1
Split the text into words and treat each word independently.
Hint 2
Uppercase the first character and lowercase the rest, then join the words back with spaces.
Hint 3
Watch for empty input so you do not try to access a missing first character.
Expected complexity
O(n) time, O(n) extra space
References
This challenge is adapted for CodeUtility. Follow the statement and constraints above.