C programming strcoll string.h


In C programming the <string.h> strcoll function compares two string based on their locality of the string.It takes into account the structural hierarchy of the alphabets of a particular language and based on that the comparison is made.The declaration of the function is given below.

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

Parameters:
s1 -The first string.

s2 -The second string.

Return type
int -Returns zero(0) if the two strings are equal.Returns value greater than 0-most probably 1-if ‘s1’ is greater than ‘s2’.Returns value lesser than 0(most probably -1) if ‘s1’ is smaller than ‘s2’.

Note before calling this function call the function “setlocale” to set the locality of the string to specific language.Based on the language set the ‘strcoll’ will compare the two strings.

In calling ‘setlocale‘ the first argument must be “LC_COLLATE” and the second argument must be some values that will set the locale to specific language.In Windows the second argument is usually a “code page” (read more about code page here https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx).This code page will set the locale to specific country or language.In the code example given below we will set the locale to Bulgaria,this means the ‘strcoll’ will compare the two strings base on the hierarchy of the Bulgarian Alphabets(not English Alphabets).In other words,the function will compare the string as though it were comparing a Bulgarian strings;you can check out the Bulgarian Alphabets from Wikipedia.

The code example is meant for Windows user.

Code example

char bl1[]=”Man” , bl2[]=”Hype” ;

int ret;

setlocale( LC_COLLATE , “.21025”) //the value 21025 is the code page for Bulgarian Alphabets

ret=strcoll(bl1 , bl2);

printf(“ret=%d” , ret);

Output in CB,

ret=-1

The return value is -1 because in Bulgarian alphabets ‘M’ comes before ‘H’ so ‘strcoll’ function considered the string s1 to be smaller than s2.Another code example is given below.



In this second code example you will clearly see how ‘strcoll’ function is affected by the locale-specific rule comparison of the string.Here we will also use the function “strcmp” to compare ths strings but which is unaffected by the locale set using the function ‘setlocale’.So ‘strcmp’ will always compare two strings as though it were comparing an English alphabets.For this second example the locale will be set to Russia;comparison of the two strings will be made by ‘strcoll’ as Russian Alphabet string.

Code example

char rs1[]=”Pen” , rs2[]=”Cat” ;

int ret;

setlocale( LC_COLLATE , “.20880”) //the value 20880 is the code page for Russian Alphabets

printf(“Calling strcoll\n”);
ret=strcoll(rs1 , rs2);

printf(“ret=%d” , ret);

printf(“\n\nCalling strcmp\n”);
ret=strcmp(rs1 , rs2);

printf(“ret=%d” , ret);

Output in CB,

Calling strcoll
ret=-1

Calling strcmp
ret=1

As you can see ‘strcoll’ returns ‘-1’,since in Russian Alphabet ‘P’ occurs before ‘C’,but in English Alphabets ‘P’ occur after ‘C’,so ‘strcmp'(which is unaffected by the locale set) returns 1.


Related links

->C strcmp string.h

->C strncmp string.h

->C strxfrm string.h

->C memcmp string.h