#include <stdio.h>
int main() {
char s1[1001], s2[1001];
scanf("%s",s1);
scanf("%s",s2);
int i = 0;
while (s1[i] == '\0' && s2[i] != '\0'&&s1[i]==s2[i]) {
i++;
}
if (s1[i] == '\0' && s2[i] == '\0') {
printf("-1\n");
} else {
printf("%d\n", i);
}
return 0;
}
This code reads two strings and tries to find the first position where they differ. It prints -1 if the strings are identical, otherwise it prints the index of the first differing character.
- The condition in the while loop is incorrect. The loop should continue while the characters are equal and neither string has ended, but your condition checks `s1[i] == '\0'` which is true only when the string has ended, so the loop never runs for non-empty strings. Think about what condition should be true for the loop to continue, not to stop.
- The logic for checking if both strings ended uses `s1[i] == '\0' && s2[i] == '\0'`, but after the loop, `i` already points to the first mismatch or the end of one string. Consider what `s1[i]` and `s2[i]` represent after the loop ends correctly.