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; 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 and tries to find the first position where they differ, but it has several logical errors. The loop condition and the initial assignments to `s1[len1]` and `s2[len2]` are incorrect.

- The lines `s1[len1]='\0';` and `s2[len2]='\0';` set the first character of each string to the null terminator, which erases the actual input. You should not modify the strings before comparing them.
- The loop condition `while(s1[i]=='\0'&&s2[i]!='\0')` is wrong. It checks if the first character of `s1` is null, which will be true after the previous assignment, and then the loop never runs. You need a condition that continues while both strings have characters and they are equal.