Dễ 1 người đã giải Tỷ lệ chấp nhận 100.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.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 851 ms - 5/5 Xem code

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

Lời giải cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.