// 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 } So I wrote a string comparison method in Cpp and I can't figure out what's wrong.
||should be&&. And then you will need to rewrite the logic a bit. In your very example in the comment above that line you will go out of bounds with thatifcondition.