First clarify the meaning of the function:
strcmp is a comparison function, and the function prototype is int strcmp(const char * src, const char * dst) The ASCIIZ value of the string is preferentially compared during the comparison process (compare from left to right until different characters or '/0' appear)
When src<dst, the return value is negative;
When src=dst, the return value is 0;
When src>dst, the return value is a positive number.
The code to implement the function is given below:
The first way:
char *my_strcmp(const char* src, const char* dst)
{
assert(src);
assert(dst);
while (*src != '/0' && *dst != '/0' && *src = *dst)
{
src++;
dst++;
if (*src && *dst)
{
if (*src > *dst)
return 1;
else
return -1;
}
}
if (*src != '/0' && *dst != '/0' && *src == *dst)
return 0;
if (*src)
return -1;
if (*dst)
return 1;
}
The second way:
char *my_strcmp(const char* src, const char* dst)
{
int ret = 0;
while (!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if (ret < 0)
ret = -1;
else if (ret > 0)
ret = 1;
return(ret);
}