This example uses strxfrm to transform two different strings that have the same collating weight. It then calls strcmp to compare the new strings.
#include <stdlib.h> #include <stdio.h> #include <locale.h> #include <string.h>
#if (1 == __TOS_OS2__) #define LOCNAME "da_dk.ibm-865" /* OS/2 name */ char *string1 = "str\xA0ng1a"; char *string2 = "strang1\x83"; #else #define LOCNAME "da_dk.ibm-1252" /* Windows name */ char *string1 = "str\xE0ng1a"; char *string2 = "strang1\xE2"; #endif
int main(void)
{
char *newstring1, *newstring2;
size_t length1, length2, pw1, pw2;
if (NULL == setlocale(LC_ALL, LOCNAME)) {
printf("Locale \"%s\" could not be loaded\n", LOCNAME);
exit(1);
}
length1 = strxfrm(NULL, string1, 0);
pw1 = strlen(string1);
length2 = strxfrm(NULL, string2, 0);
pw2 = strlen(string2);
if (NULL == (newstring1 =(char*) calloc(length1 + 1, 1)) ||
NULL == (newstring2 =(char*) calloc(length2 + 1, 1))) {
printf("insufficient memory\n");
exit(1);
}
/* Get primary weight of each string */
if ((strxfrm(newstring1, string1, pw1 + 1) != length1) ||
(strxfrm(newstring2, string2, pw2 + 1) != length2)) {
printf("error in string processing\n");
exit(1);
}
if (0 != strcmp(newstring1, newstring2))
printf("wrong results\n");
else
printf("correct results\n");
return 0;
/**********************************************************
The output should be similar to :
correct results **********************************************************/ }