简体   繁体   中英

My String compare isn't working in Cpp

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

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

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.)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM