Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Title Case

Capitalize the first letter of every word.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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ụ

Ví dụ 1
Đầ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".

Ví dụ 2
Đầ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.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.