← Back to Streams
Question 581
Count Words
Count Words
A text file contains words separated by any mix of spaces and newlines, possibly with extra blank space, for example: the quick brown fox jumps Write a functionint countWords(const string& filename)that returns how many words the file contains; a word is any run of non-whitespace characters. The starter opens the file and declaresstring wordandint count; write the loop. The test prints the returned count on its own line, for example (see Expected Output). Do NOT write a main function.
Expected Output:
5 3
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Streamsifstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.