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
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution