Đề bài
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.
Ràng buộc
0 ≤ length ≤ 10,000 (letters and spaces)
Ví dụ
Đầu vào:
text = "hello world"
Kết quả mong đợi:
expected = "Hello World"
Đầu vào:
std::string text = "hello world";
Kết quả mong đợi:
std::string expected = "Hello World";
Đầu vào:
string text = "hello world";
Kết quả mong đợi:
string expected = "Hello World";
Giải thích
"hello" → "Hello" and "world" → "World".
Đầu vào:
text = "tHE qUICK fox"
Kết quả mong đợi:
expected = "The Quick Fox"
Đầu vào:
std::string text = "tHE qUICK fox";
Kết quả mong đợi:
std::string expected = "The Quick Fox";
Đầu vào:
string text = "tHE qUICK fox";
Kết quả mong đợi:
string expected = "The Quick Fox";
Giải thích
Remaining letters become lowercase: "The Quick Fox".
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Split the text into words and treat each word independently.
Gợi ý 2
Uppercase the first character and lowercase the rest, then join the words back with spaces.
Gợi ý 3
Watch for empty input so you do not try to access a missing first character.
Độ phức tạp mong đợi
O(n) time, O(n) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.