Online Compiler C

#include <stdio.h> int main() { char s1[1001], s2[1001]; int is_ok = -1; fgets(s1, 1001, stdin); fgets(s2, 1001, stdin); for (int i = 0; s1[i] != '\0' && s2[i] != '\0'; i++) { if (s1[i] != s2[i]) { is_ok = i; break; } } printf("%d\n", is_ok); return 0; }
This code reads two strings and compares them character by character. It sets `is_ok` to the index of the first difference, or leaves it as -1 if the strings are identical up to the shorter string's length. However, it does not handle the case where one string is a prefix of the other (e.g., "abc" and "abcd").

- The loop condition stops when either string reaches its null terminator. This means if one string is shorter, the loop ends without detecting the difference in length. Consider what should happen when the strings have different lengths but match for all characters of the shorter one.
- The variable name `is_ok` is misleading because it stores the index of the first mismatch, not a success flag. Think about what value should be printed when the strings are completely equal, including when they have the same length.