Условие
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.
Ограничения
0 ≤ length ≤ 10,000 (letters and spaces)
Примеры
Ввод:
text = "hello world"
Ожидаемый вывод:
expected = "Hello World"
Ввод:
std::string text = "hello world";
Ожидаемый вывод:
std::string expected = "Hello World";
Ввод:
string text = "hello world";
Ожидаемый вывод:
string expected = "Hello World";
Пояснение
"hello" → "Hello" and "world" → "World".
Ввод:
text = "tHE qUICK fox"
Ожидаемый вывод:
expected = "The Quick Fox"
Ввод:
std::string text = "tHE qUICK fox";
Ожидаемый вывод:
std::string expected = "The Quick Fox";
Ввод:
string text = "tHE qUICK fox";
Ожидаемый вывод:
string expected = "The Quick Fox";
Пояснение
Remaining letters become lowercase: "The Quick Fox".
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
Split the text into words and treat each word independently.
Подсказка 2
Uppercase the first character and lowercase the rest, then join the words back with spaces.
Подсказка 3
Watch for empty input so you do not try to access a missing first character.
Ожидаемая сложность
O(n) time, O(n) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.