-2

I'm a little confused with the string compare strcmp() function in C.

When you have two strings, grass and grapes and you use strcmp(grass, grapes); which results in 39, or any positive number, does this mean that "grapes" is alphabetized before "grass", or the opposite?

I know that if it results to 0, they're equal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WTL
  • 103
  • 1
  • 3
  • 9
  • 4
    You mean `strcmp("grass", "grapes")`? if *less than zero* grass sorts before grapes, if *equal to zero*, they are equal, if *greater than zero* grapes sorts before grass. – David C. Rankin Nov 17 '15 at 23:13
  • look at [this link](http://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm). under Return Value – R Nar Nov 17 '15 at 23:14
  • You can use `man strcmp` if you're on linux. – Jason Nov 17 '15 at 23:15

2 Answers2

0

The return value of strcmp is defined in C99 7.21.4

The sign of a nonzero value returned by the comparison functions memcmp, strcmp, and strncmp is determined by the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the objects being compared. So if the result is positive, it means the second argument comes after the first.

It's not exactly alphabetical order, but is rather dependent on the underlying encoding of the characers. For instance, in ASCII, 'B' < 'a', because 'B' is encoded as 66 and 'a' is 97. If the characters are all letters of the same case, this will be equivalent to alphabetical order in all (non-multibyte) encodings I'm familiar with, but I don't believe this is required.

For cases like "grass" vs "grapes", it'll just keep scanning until it finds characters that differ ('s' vs 'p' in this case), and then make the decision. A special case of this is when one string is a substring of another: e.g. "grape" vs "grapes". For that case, you just need to remember that "grape" is actually { 'g', 'r', 'a', 'p', 'e', '\0' }, and apply the normal rule: '\0' < 's', so "grape" comes before "grapes".

This would be a conforming implementation of strcmp:

int strcmp(const char *a, const char *b) {
    size_t i = 0;
    while (a[i] || b[i]) {
        if (a[i] != b[i]) {
            if (a[i] < b[i]) return -1;
            else return 1;
        }
        i++;
    }
    return 0;
}
Ray
  • 1,706
  • 22
  • 30
0

strcmp function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

This means that, this function performs a binary comparison of the characters.

The following program should give you an Idea about how strcmp works:

#include <stdio.h>
#include <string.h>

int stringcmp(char *s1, char *s2){
    int count = 0;

    while (s1[count] == s2[count]) {
        if (s1[count] == '\0' || s2[count] == '\0')
            break;
            count++;
    }

    if (s1[count] == '\0' && s2[count] == '\0'){
        return 0;
    }

    if(strlen(s1) < strlen(s2)){
        return -1;
    }else{
        return 1;
    }
}

int main(void){
    char *b = "grass";
    char *a = "grapes";

    if(stringcmp(a, b) == 0){
        printf("Are equal.\n");
        printf("Length of A = %zu\n",strlen(a));
        printf("Length of B = %zu\n",strlen(b));
        printf("Return of stringcmp = %d\n",stringcmp(a, b));
    }else{
        printf("Are not equal.\n");
        printf("Length of A = %zu\n",strlen(a));
        printf("Length of B = %zu\n",strlen(b));
        printf("Return of stringcmp = %d\n",stringcmp(a, b));
    }

   return 0;
}

Output:

Are not equal.
Length of A = 5
Length of B = 6
Return of stringcmp = -1

If you swap a with b you get:

Are not equal.
Length of A = 6
Length of B = 5
Return of stringcmp = 1

And if A and B are the same:

Are equal.
Length of A = 5
Length of B = 5
Return of stringcmp = 0
Michi
  • 5,175
  • 7
  • 33
  • 58