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 } 

So I wrote a string comparison method in Cpp and I can't figure out what's wrong.

6
  • 2
    The || 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 that if condition. Commented Oct 31, 2013 at 15:56
  • I was going to say state the error, but I'm betting Simple has the right of it. Commented Oct 31, 2013 at 15:57
  • 1
    Also, if the two string has different length? Commented Oct 31, 2013 at 15:58
  • 1
    In what input it is not working? any, or special cases? Is there false positive, false negative or both? Commented Oct 31, 2013 at 15:59
  • when I run the method on 2 strings '\0' and 'foobar\0' it returns 0 instead of difference between f and '\0' Commented Oct 31, 2013 at 16:01

3 Answers 3

1

As long as others are showing code, here is my take on this. There is no need to continuously keep on comparing the first and second character to 0, and then to each other. As soon as one of the two characters is 0, you are done and you can return the difference r. r need not to be initialized because the second part of the while test is always executed -- it's an and so both parts need to be true.


Also noteworthy: I see I instinctively reversed the sign of the result. When comparing string A to B, you might want to know if "A is smaller than B", and that would be indicated by a negative result.

#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; } 
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I can see, your function is fine. In particular, it does work on the example where you say it doesn't:

#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")); } 

This prints a negative number as one would expect.

(I've renamed the function so that there is no confusion which strcmp() is getting called. I recommend you do likewise.)

Comments

0

This is the working example:

// 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 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.