2使用strcmp函数测试失败

Joe*_*Joe 0 c++ strcmp

下面的代码在我的头文件中:

int mystrcmp(const char *s1, const char *s2) // strcmp function
{
    while(*s1 == *s2)
    {
        if(*s1 == '\0' || *s2 == '\0')
            break;

        s1++;
        s2++;
    }

    if(*s1 == '\0' && *s2 == '\0')
        return (0);
    else
        return (-1);
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我运行它时,我的main.cpp表示它未通过2次测试

以下是我的main.cpp的摘录:

void testmystrcmp(void)
{
   int iResult;

   iResult = mystrcmp("Ruth", "Ruth");
   ASSURE(iResult == 0);

   iResult = mystrcmp("Gehrig", "Ruth");
   ASSURE(iResult < 0);

   iResult = mystrcmp("Ruth", "Gehrig");
   ASSURE(iResult > 0);  // right here mystrcmp fails the test

   iResult = mystrcmp("", "Ruth");
   ASSURE(iResult < 0);

   iResult = mystrcmp("Ruth", "");
   ASSURE(iResult > 0);

   iResult = mystrcmp("", "");
   ASSURE(iResult == 0); // it also fails the test here but why??
}
Run Code Online (Sandbox Code Playgroud)

注意:我无法更改.cpp文件

我一直试图解决这个问题,但不知道如何解决.

Mat*_*son 5

strcmp如果"first"字符串大于"second"字符串,则定义为返回正值;如果"first"字符串相等则定义为0,如果"first"小于"second"字符串则返回负值.因此,如果字符串不相等,您应该决定哪一个更大,然后返回适当的值.

实现这一目标的一种简单方法是返回*s1 - *s2(当它们相等时也返回0,作为奖励).