← Back to Streams
Question 586
Line Containing a Word
Line Containing a Word
Write a functionint lineOf(const string& filename, const string& word)that returns the 1-based number of the first line of the file that containswordanywhere inside it, or -1 if no line contains it. For a file containing: apple pie cherry tart banana breadlineOf(..., "cherry")returns 2 andlineOf(..., "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
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.