Compare two versions of a file that have the same number of lines, the way git diff shows changes. Print an unchanged line with two spaces in front. For a changed line, print the old line with - in front and then the new line with + in front.
Input
The first line has n (1 ≤ n ≤ 50). The next n lines are the old version, then n lines are the new version. Each line has at most 80 characters and may contain spaces.
Output
2n lines or fewer, as described.
Examples
4 total = 0 for each mark total += mark print total / n total = 0 for each mark total += mark print (double) total / n
total = 0 for each mark total += mark - print total / n + print (double) total / n
1 same same
same
Hints
Hint 1
After scanf("%d", &n), the newline is still waiting: read it with getchar() before the first fgets.
Hint 2
fgets keeps the newline at the end of the line; compare whole lines with strcmp.
Hint 3
Store the old lines in a 2D char array, then compare each new line with the old line at the same position.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>
char old[50][100];
int main(void) {
int n;
char line[100];
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) fgets(old[i], sizeof old[i], stdin);
for (int i = 0; i < n; i++) {
fgets(line, sizeof line, stdin);
if (strcmp(old[i], line) == 0) {
printf(" %s", line);
} else {
printf("- %s", old[i]);
printf("+ %s", line);
}
}
return 0;
}