Online Compiler C

#include <stdio.h> int main() { char s1[1001], s2[1001]; fgets(s1, sizeof(s1), stdin); fgets(s2, sizeof(s2), stdin); int len1 = 0; int len2 = 0; while (s1[len1] != '\0' && s1[len1]!='\n')len1++ ; while (s2[len2] != '\0' && s2[len2]!='\n')len2++ ; s1[len1]='\0'; s2[len2]='\0'; int i=0; while(s1[i]=='\0'&&s2[i]!='\0'){ if (s1[i] != s2[i]) { printf("%d\n", i); return 0; } i++; } if (s1[i] == '\0' && s2[i] == '\0') { printf("-1\n"); } else { printf("%d\n", i); } return 0; }
This code reads two strings, removes the newline characters from each, and then tries to find the first position where the strings differ. It prints that index, or -1 if the strings are identical.

- The while loop condition `while(s1[i]=='\0'&&s2[i]!='\0')` is incorrect. This condition checks if the first string is at its end while the second is not, but it should be checking if the characters are equal, not if one is null. The loop should continue while both strings have characters and those characters are the same.
- After the loop, the logic for printing the result is also off. You need to check whether the loop ended because one string ended or because a difference was found, and then print the appropriate index or -1.