ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Title Case

Capitalize the first letter of every word.

สิ่งที่คุณจะได้เรียนรู้

  • Change the case of single characters
  • Process each word of a sentence
  • Join words back into one string

โจทย์

ดูฉบับแปล

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)

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
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".

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

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก