strcoll in C

In this blog post, you will learn what is the strcoll() and how to use the strcoll() function in C to compare two strings with the help of programming examples.

 

What is strcoll() in C?

The strcoll() function is declared in <string.h> header file. It compares the string pointed to by s1 to the string pointed to by s2. The comparison is based on the current locale defined by the LC_COLLATE category.

Now you are thinking if we have already memcmp,strcmp, and strncmp then what is the use of strcoll().

I respect your thinking and you are right in the “C” locale strcmp() is enough for string comparisons. The reason is that in the “C” locale, the order of characters in the ASCII character set is the same as the lexicographic order of the characters.

But it is not true for all languages, in certain European code pages, for example, the character 'a' (value 0x61) precedes the character 'ä' (value 0xE4) in the character set, but the character 'ä' precedes the character 'a' lexicographically.

In such a case for a lexicographic comparison, we should use the strcoll because it takes the bytes, transforms them using the locale, then compares the result. On the other hand strcmp() takes the bytes of the string one by one and compares them as is whatever the bytes are. The strcmp does not evaluate the correct result in that cases.

Note: In place of strcoll() you can use strxfrm() on the original strings, then use strcmp on the resulting strings.

 

Syntax strcoll in C:

The following is the syntax of the strcoll function in C.

int strcoll(const char *s1, const char *s2);

 

strcoll Parameters:

The strcoll() the function accepts the following parameters:

s1 — pointer to the null-terminated byte string.

s2 — pointer to the null-terminated byte string.

 

strcoll return value:

The strcoll() returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2 when both are interpreted as appropriate to the current locale.

See the below table for a better understanding.

Return Value               Meaning
greater than zero      s1 is greater than str2
zero                   s1 is equal to s2
less than zero         s1 is less than s2

 

 

Example program to describe how to use strcoll in C:

The following program illustrates the working of the strcoll() function in the C language.

Case 1: string1 greater than string2.

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

int main()
{
    char s1[] = "aml";
    char s2[] = "AML";

    int ret = strcoll(s1, s2);

    if (ret > 0)
    {
        printf("s1 is greater than s2");
    }
    else if (ret < 0)
    {
        printf("s1 is lesser than s2");
    }
    else
    {
        printf("s1 is equal to s2");
    }

    return (0);
}

Output: s1 is greater than s2

In the above code strcoll() function returns an integer value greater than zero. The reason is that the character 'A' (value 0x41) precedes the character 'a' (value 0x61).

 

Case 2: string1 less than string2.

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

int main()
{
    char s1[] = "AML";
    char s2[] = "aml";

    int ret = strcoll(s1, s2);

    if (ret > 0)
    {
        printf("s1 is greater than s2");
    }
    else if (ret < 0)
    {
        printf("s1 is lesser than s2");
    }
    else
    {
        printf("s1 is equal to s2");
    }

    return (0);
}

Output: s1 is lesser than s2

 

Case 3: string1 equal to string2.

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

int main()
{
    char s1[] = "AML";
    char s2[] = "AML";

    int ret = strcoll(s1, s2);

    if (ret > 0)
    {
        printf("s1 is greater than s2");
    }
    else if (ret < 0)
    {
        printf("s1 is lesser than s2");
    }
    else
    {
        printf("s1 is equal to s2");
    }

    return (0);
}

Output: s1 is equal to s2

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *