← Back to Streams
Question 621
Word Counts to File
Word Counts to File
Write a functionvoid writeWordCounts(const string& src, const string& dst)that reads every whitespace-separated word from the filesrc, counts how many times each word appears, and writes the result to the filedst(created or truncated): one line per distinct word, in alphabetical order, as the word, a space, its count, then a newline. Words are compared exactly (case-sensitive). Use amap<string, int>. For a source file containingapple pear applethe filedstmust contain exactly: apple 2 pear 1 Do NOT write a main function.
Expected Output:
apple 2 pear 1 a 2 b 2 c 1
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsofstream
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.