// 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中写了一个字符串比较的方法,我无法弄清楚是什么问题。