I am new to the C language. Please, could someone tell me why I am always getting zero as output when comparing differing strings using my own implementation of strcmp?
I wrote the function xstrcmp to compare two strings: if they are equal, then it returns 0; otherwise, it returns the numeric difference between the ASCII values of the first non-matching pair of characters.
#include<stdio.h>
int xstrcmp(char*,char*);
int main()
{
int i;
char string1[]="jerry";
char string2[]="ferry";
i=xstrcmp(string1,string2);
printf("difference=%d\n",i);
return 0;
}
int xstrcmp(char*p,char*q)
{
int m;
while(*p!=*q)
{
if((*p=='\0')&&(*q=='\0'))
break;
p++;
q++;
}
m=(*p)-(*q);
return m;
}
while(*p!=*q)