#K40462. Count Words in a String
Count Words in a String
Count Words in a String
This problem requires you to write a program that counts the number of words in a given string. A word is defined as a sequence of non-space characters delimited by one or more spaces. In other words, if you split the string by spaces, the number of resulting non-empty tokens is the answer.
You can express the process mathematically as: $$words = \text{split}(s)$$ where each word is a maximal sequence of characters not containing any white-space.
Be careful to handle cases with extra spaces, leading or trailing spaces, and even empty strings.
inputFormat
The input consists of a single line containing a string s
. The string may include spaces, punctuation, or be empty. You should read the entire line from standard input.
outputFormat
Output a single integer representing the number of words in the input string. Print the result to standard output.
## sampleHello, World!
2