Word Count
InterviewTime limit2sMemory limit128 MB
Count the number of space-separated words in a string that may have leading or trailing spaces.
- Level
Easy2 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem
Given a string consisting of uppercase English letters, lowercase English letters, and spaces, determine how many words it contains. If the same word appears more than once, count each occurrence separately.
Input
The first line contains a string consisting of uppercase English letters, lowercase English letters, and spaces. Its length is at most 1,000,000.
Words are separated by exactly one space. Spaces never appear consecutively, but the string may start or end with a space.
Output
Print the number of words in the string.