C String Functions - C strcoll






Compares two strings using the current locale's collating order.

int strcoll(const char *str1 , const char *str2 );

Compares string str1 to str2. The result is dependent on the LC_COLLATE setting of the location.

Returns zero if str1 and str2 are equal. Returns less than zero or greater than zero if str1 is less than or greater than str2 respectively.

Ptototype

int strcoll( const char *lhs, const char *rhs );

Parameters

lhs, rhs - pointers to the null-terminated byte strings to compare

Return value

Negative value if lhs is less than (precedes) rhs.

0 if lhs is equal to rhs.

Positive value if lhs is greater than (follows) rhs.

Example


#include <stdio.h>
#include <string.h>
#include <locale.h>
 //ww  w .j a va2 s.  co  m
int main(void)
{
    setlocale(LC_COLLATE, "cs_CZ.iso88592");
 
    const char* s1 = "asdf";
    const char* s2 = "asdf";
 
    printf("In the Czech locale: ");
    if(strcoll(s1, s2) < 0)
         printf("%s before %s\n", s1, s2);
    else
         printf("%s before %s\n", s2, s1);
 
    printf("In lexicographical comparison: ");
    if(strcmp(s1, s2) < 0)
         printf("%s before %s\n", s1, s2);
    else
         printf("%s before %s\n", s2, s1);
}

The code above generates the following result.