// 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에서 문자열 비교 방법을 작성 했으므로 잘못된 것이 무엇인지 알 수 없습니다.내 문자열 비교가 Cpp에서 작동하지 않습니다.
'||'는 '&&'여야합니다. 그리고 나서 로직을 약간 다시 작성해야합니다. 그 라인 위의 주석에있는 당신의 예에서, 당신은 그'if' 조건으로 범위를 벗어날 것입니다. – Simple
나는 그 상태를 진술한다고 말하려고했으나, 나는 단순함이 그것의 권리를 가진다는 것을 내기하고있다. – IllusiveBrian
또한 두 문자열의 길이가 다른 경우? – Matt