Line Containing a Word
mediumQuestion 586Write 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, so 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.