CJCoding With Joseph
15free / daySign inCreate account

First Differing Line

Write a function int firstDifference(const string& a, const string& b) that compares two text files line by line and returns the 1-based number of the first line where they differ, or 0 if the files are identical. If one file ends before the other, the first line that exists in only one of them is the first difference.

Examples (each test creates both files):
- lines x, y, z versus x, Q, z returns 2
- lines x, y, z versus x, y, z returns 0
- lines a, b versus a, b, c returns 3

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

Do NOT write a main function.

Expected Output:

2
0
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.