← Back to Streams
Question 590
Count a Character with get
Count a Character with get
Write a functionint countChar(const string& filename, char c)that returns how many times the charactercappears in the file. Read the file one character at a time with the stream'sgetmember; spaces and newlines count. For a file containing: mississippicountChar(..., 's')returns 4. For the lineone two three(two spaces beforethree),countChar(..., ' ')returns 3. The test prints the returned count on its own line, for example (see Expected Output). Do NOT write a main function.
Expected Output:
4 3
= newline. The output ends with a newline — print one after the last line.
Hints4 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.