CJCoding With Joseph
15free / daySign inCreate account

Line Containing a Word

Write a function int lineOf(const string& filename, const string& word) that returns the 1-based number of the first line of the file that contains word anywhere inside it, or -1 if no line contains it. For a file containing:

apple pie
cherry tart
banana bread

lineOf(..., "cherry") returns 2 and lineOf(..., "kiwi") returns -1.

The test prints the returned number on its own line, for example (see Expected Output).

Do NOT write a main function.

Expected Output:

2
-1

= 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.