Đề bài
Xem bản dịchReturn the longest word in text (words are separated by spaces). If several words share the longest length, return the first one.
Implement longest_word(text).
Input
text: The sentence.
Output
Return the longest word, or an empty string if there are no words.
Ràng buộc
0 ≤ length ≤ 10,000
Ví dụ
Ví dụ 1
Đầu vào:
text = "I love programming"
Kết quả mong đợi:
expected = "programming"
Đầu vào:
std::string text = "I love programming";
Kết quả mong đợi:
std::string expected = "programming";
Đầu vào:
string text = "I love programming";
Kết quả mong đợi:
string expected = "programming";
Giải thích
"programming" has 11 letters, more than the others.
Ví dụ 2
Đầu vào:
text = "cat dog cow"
Kết quả mong đợi:
expected = "cat"
Đầu vào:
std::string text = "cat dog cow";
Kết quả mong đợi:
std::string expected = "cat";
Đầu vào:
string text = "cat dog cow";
Kết quả mong đợi:
string expected = "cat";
Giải thích
All are 3 letters; the first, "cat", wins.
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.