โจทย์
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน