Your problem is that you do not detect the end of the line and therefore do not return zero if both lines end before any difference is detected.
You can simply fix this by checking this in the loop condition:
while( flag==0 && (string1[i] != 0 | string2[i] != 0 ) )
Note that both lines are checked, because if only one is at the end, the lines are not equal, and comparisons within the loop should detect this.
Please note that comparing characters may not give the result you expect. For one, he didnβt determine whether the char signed or unsigned, so you should probably use unsigned char for comparison.
Perhaps a cleaner solution would be to immediately return when you find the difference, and instead of flag = -1 you will return -1 directly. But it is rather a matter of opinion.
skyking
source share