C language simulation realizes strcmp() of standard library function

strcmp()
C/C++函数,比较两个字符串
设这两个字符串为str1,str2,
若str1==str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。

char * my_strcmp(char *key, char *buffer)
{
    int ret = 0;
    while ((ret = *key - *buffer)== '\0' && *key != '\0')
    {
        key++;
        buffer++;
    }
    if (ret < 0)
    {
        return -1;
    }
    else if (ret > 0)
    {
        return 1;
    }
    return 0;
}
int main()
{
    char key[] = "apple";
    char buffer[]="apple";
    printf("%s\n%s\n%d\n", key,buffer,my_strcmp(key, buffer));
    system("pause");
    return 0;
}

write picture description here

write picture description here
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325594487&siteId=291194637