1
// Compares the two arguments.  If they are equal, 0 is returned.  If they
// are not equal, the difference of the first unequal pair of characters
// is returned.
int strcmp(const char* charPointerToArray1, const char* charPointerToArray2) {
    int i = 0;
    // we need to check if both arrays have reached their terminating character
    // because consider the case where array1 = { '\0' } and array2 = { '1', '\0' }
    while (charPointerToArray1[i] != '\0' || charPointerToArray2[i] != '\0') {
        // while iterating through both char arrays,
        // if 1 char difference is found, the 2 char arrays are not equal
        if (charPointerToArray1[i] != charPointerToArray2[i]) {
            int differenceOfFirstUnequalChars = charPointerToArray1[i] - charPointerToArray2[i];
            return differenceOfFirstUnequalChars;
        } else {
            i++;
        }
    }
    return 0; // charPointerToArray1 == charPointerToArray2
}

所以我在Cpp中写了一个字符串比较的方法,我无法弄清楚是什么问题。

4

3 回答 3

1

只要其他人正在显示代码,这就是我对此的看法。没有必要不断地将第一个和第二个字符与 0 进行比较,然后再相互比较。只要两个字符之一为 0,您就完成了,您可以返回差值rr不需要初始化,因为while测试的第二部分总是被执行——它是一个所以两个部分都需要是真的。


同样值得注意的是:我看到我本能地反转了结果的符号。在比较字符串 A 和 B 时,您可能想知道“A 是否小于B”,这将由否定结果指示。

#include <stdio.h>

int my_strcmp (const char* charPointerToArray1, const char* charPointerToArray2)
{
    int i = 0, r;
    while ((charPointerToArray1[i] || charPointerToArray2[i]) &&
        !(r = (charPointerToArray2[i] - charPointerToArray1[i])))
    {
        i++;
    }
    return r;
}

int main (void)
{
    printf("%d\n", my_strcmp("foobar", ""));
    printf("%d\n", my_strcmp("foobar", "foobaz"));
    printf("%d\n", my_strcmp("foobar", "foobar"));
    return 0;
}
于 2013-10-31T16:20:21.640 回答
0

这是工作示例:

// Compares the two arguments.  If they are equal, 0 is returned.  If they
// are not equal, the difference of the first unequal pair of characters
// is returned.
int strcmp(const char* charPointerToArray1, const char* charPointerToArray2) {
    int i = 0;
    // we need to check if both arrays have reached their terminating character
    // because consider the case where array1 = { '\0' } and array2 = { '1', '\0' }
    while (charPointerToArray1[i] != '\0' && charPointerToArray2[i] != '\0') {
        // while iterating through both char arrays,
        // if 1 char difference is found, the 2 char arrays are not equal
        if (charPointerToArray1[i] != charPointerToArray2[i]) {
            int differenceOfFirstUnequalChars = charPointerToArray1[i] - charPointerToArray2[i];
            return differenceOfFirstUnequalChars;
        } else {
            i++;
        }
    }
    // return 0; // not really, one of the array may be longer than the other
    if (charPointerToArray1[i] == '\0' && charPointerToArray2[i] == '\0')
        return 0;
    else //... one of the array is longer
}
于 2013-10-31T16:05:49.273 回答
0

据我所知,你的功能很好。特别是,它确实适用于您说它不适用的示例:

#include <stdio.h>

int my_strcmp(const char* charPointerToArray1, const char* charPointerToArray2) {
    int i = 0;
    // we need to check if both arrays have reached their terminating character
    // because consider the case where array1 = { '\0' } and array2 = { '1', '\0' }
    while (charPointerToArray1[i] != '\0' || charPointerToArray2[i] != '\0') {
        // while iterating through both char arrays,
        // if 1 char difference is found, the 2 char arrays are not equal
        if (charPointerToArray1[i] != charPointerToArray2[i]) {
            int differenceOfFirstUnequalChars = charPointerToArray1[i] - charPointerToArray2[i];
            return differenceOfFirstUnequalChars;
        } else {
            i++;
        }
    }
    return 0; // charPointerToArray1 == charPointerToArray2
}


int main() {
  printf("%d\n", my_strcmp("", "foobar"));
}

正如人们所期望的那样,这将打印一个负数。

(我已重命名该函数,以免被调用时混淆strcmp()。我建议您也这样做。)

于 2013-10-31T16:05:01.403 回答