71. Count the Words
Core1000 ms256 MBSolved by 0%
Read one line and print how many words it contains.
A word is any run of non-space characters. Words may be separated by more than one space, and the line may begin or end with spaces.
Constraints - The line has between 1 and 100000 characters. - Printable ASCII only.
Input
A single line of text.
Output
Print one integer, how many words the line contains. Print 0 for a line with no words.
Inputhello world
Output2
Notehas two words with a single space between them
Input hello world
Output2
Notehas leading, trailing and repeated spaces, which a naive "count the spaces plus one" gets wrong
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.