Loading...
Loading...
Loading...
A feedback form limits answers by words, not letters. People often type extra spaces at the start, at the end or between words. Read one line and print how many words it has. A word is a run of characters without spaces. A line of only spaces has 0 words.
Example 1
Input
I love Java
Output
3
Example 2
Input
extra spaces here
Output
3
Leading, trailing and repeated spaces do not make extra words.
Try the problem first. Open one hint at a time.
trim() removes the spaces at both ends.
split("\\s+") splits on one or more spaces, so repeated spaces do not make empty words.
A trimmed empty line still gives one (empty) piece after split, so check isEmpty() first.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor