Name
strcoll
Synopsis
Compares two strings by locale-specific sorting criteria
#include <string.h> intstrcoll( const char *s1, const char *s2);
Like strcmp(), the strcoll() function performs a
character-by-character comparison of the two strings,
s1 and s2.
However, where strcmp() just
compares unsigned character values, strcoll() can apply a locale-specific set
of rules in comparing strings. The value of the locale information
category LC_COLLATE determines
the applicable rule set, and can be changed by the setlocale() function.
The return value of strcoll() indicates the result of the
comparison as follows:
- Zero
The two strings are equal.
- Greater than zero
The string addressed by
s1is greater than the string addressed bys2.- Less than zero
The string addressed by
s1is less than the string addressed bys2.
Example
char *samples[ ] = { "curso", "churro" };
setlocale( LC_COLLATE, "es_US.UTF-8" );
int result =strcoll( samples[0], samples[1] );
if ( result == 0 )
printf( "The strings \"%s\" and \"%s\" are alphabetically equivalent.\n",
samples[0], samples[1] );
else if ( result < 0 )
printf( "The string \"%s\" comes before \"%s\" alphabetically.\n",
samples[0], samples[1] );
else if ( result > 0 )
printf( "The string \"%s\" comes after \"%s\" alphabetically.\n",
samples[0], samples[1] );Because the letter ch
comes after the letter c in the
Spanish alphabet, the preceding code prints this line in the
es_US locale:
The string "curso" comes before "churro" alphabetically.
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access