CJCoding With Joseph
15free / daySign inCreate account

Count a Character with get

Write a function int countChar(const string& filename, char c) that returns how many times the character c appears in the file. Read the file one character at a time with the stream's get member; spaces and newlines count. For a file containing:

mississippi

countChar(..., 's') returns 4. For the line one two  three (two spaces before three), 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
Topics:
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.