Đề bài
Xem bản dịchA word is a run of non-space characters. Count the words in text; there may be extra spaces at the start, end or between words.
Implement count_words(text).
Input
text: The text to count.
Output
Return the number of words.
Ràng buộc
0 ≤ length ≤ 10,000
Ví dụ
Ví dụ 1
Đầu vào:
text = "Learn to code today"
Kết quả mong đợi:
expected = 4
Đầu vào:
std::string text = "Learn to code today";
Kết quả mong đợi:
long long expected = 4;
Đầu vào:
string text = "Learn to code today";
Kết quả mong đợi:
long expected = 4;
Giải thích
Four words separated by spaces: 4.
Ví dụ 2
Đầu vào:
text = " "
Kết quả mong đợi:
expected = 0
Đầu vào:
std::string text = " ";
Kết quả mong đợi:
long long expected = 0;
Đầu vào:
string text = " ";
Kết quả mong đợi:
long expected = 0;
Giải thích
Only spaces, so there are no words: 0.
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.