The strcmp_kr function is based on the string compare function from K&R.
#include<stdio.h>
#include<string.h>
int strcmp_kr (char *s, char *d) {
int i=0;
while ((s[i] == d[i])) {
printf("Entered while loop\n");
if (s[i] == '\0')
return 0;
i++;
}
return s[i] - d[i];
}
int main() {
char s1[15];
char s2[15];
printf("Enter string no. 1:");
scanf("%s", s1);
printf("Enter string no. 2:");
scanf("%s", s2);
strcmp_kr(s1, s2) == 0 ? printf("Strings equal!\n") : \
printf("Strings not equal by %d!\n", strcmp_kr(s1, s2));
}
Output:
$ ./a.out
Enter string no. 1:modest
Enter string no. 2:modesy
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Entered while loop
Strings not equal by -5!
Question: Why is the while loop entering 10 times instead of 5?
scanf()without specifying field width otherwise it has a risk of buffer overrun, it doesn't exactly concern your problem though. I mentioned it as a precaution :)