Home > database >  Why does this char* comparison function return -1 when the two input strings are the same? Language:
Why does this char* comparison function return -1 when the two input strings are the same? Language:

Time:11-19

I made a function that is supposed to help me order strings in alphabetical order: it should return -1 if the first string comes before the second one, 0 if they are the same string and 1 if the first string comes after the second one. This is the code:

`

int testString(char* a, char* b){
    int i = 0;
    
    while(a[i] != EOF && b[i] != EOF){
        if(a[i] > b[i]){
            return 1;
        }
        else{
            if(a[i] < b[i]){
                return -1;
            }
            else{
                i  ;
            }
        }
    }
    
    if(a[i] == EOF && b[i]!=EOF){
        return -1;
    }
    else{
        if(a[i] != EOF && b[i] == EOF){
            return 1;
        }
        else{
            if(a[i] == EOF && b[i] == EOF){
                return 0;
            }
            else{
                printf("Problem in the string comparison\n");
                return -2;
            }
        }
    }
}

`

So: it does a while cycle as long as there are characters in the string and, if one string is the same as the other, but longer, it should return 1, -1 in the very opposite case and 0 if they are the very same, so same length. Though, this returns -1 with the very same strings and i don't get why.

I tried using it to order an array of structs of which one element was of type char* and it does work (because the sorting algorithm is a quick sort, and the one i made doesn't make a distinction between less than and equal to, during the sorting). I later printed out the strings of the array of structs and they were indeed ordered. Problem is that the testing (which used this same function) to see whether or not they were in order told me that they weren't, as it returned -1 instead of 0, when the strings were the same, so the anti-alphabetical order wasn't respected. Now i looked at the function, but it looks OK and i don't get what's wrong with it

CodePudding user response:

From the C Standard (7.21 Input/output <stdio.h>)

EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;

Usually this macro is expanded to -1.

But you need to compare strings. Strings are sequences of characters that ended with the terminating zero character '\0'

So this while loop

while(a[i] != EOF && b[i] != EOF){

can invoke undefined behavior.

Also this else statement

        else{
            printf("Problem in the string comparison\n");
            return -2;
        }

will never get the control. So it is redundant.

And introducing the variable i of the type int

int i = 0;

is also redundant.

Your function can look the following way

int testString( const char *a, const char *b )
{
    while ( *a != '\0' && *a == *b )
    {
          a;
          b;
    }

    return ( *( unsigned char * )b < *( unsigned char * )a ) - 
           ( *( unsigned char * )a < *( unsigned char * )b );
}

CodePudding user response:

while(a[i] != EOF && b[i] != EOF){

The string terminator is 0, not EOF. Change that to

while (a[i] != 0 && b[i] != 0 )

or just

while (a[i] && b[i])

Do the same in the remainder of your code where you're comparing against a[i] and b[i] against EOF.

There is no EOF character as such - EOF is the value returned by input functions if they detect an end-of-file condition on a stream.

  • Related