// 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中写了一个字符串比较方法,我无法弄清楚是什么问题。
答案 0 :(得分:1)
只要其他人显示代码,这是我对此的看法。不需要继续将第一和第二字符比较为0,然后相互比较。 只要两个字符中的一个为0,您就完成了,您可以返回差异r
。 r
无需初始化,因为while
测试的第二部分始终执行 - 它是和,因此两个部分都必须为true。
同样值得注意的是:我看到我本能地扭转了结果的征兆。将字符串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;
}
答案 1 :(得分: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()
被调用。我建议你这样做。)
答案 2 :(得分: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
}