Example (wcsxfrm -- Transform Wide-Character String)

This example uses wcsxfrm to transform two different strings with the same collating weight. It then uses wcscmp to compare the new strings.

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#if (1 == __TOS_OS2__)
   #define LOCNAME "da_dk.ibm-865"        /* OS/2 name */
   wchar_t *string1 = L"str\xA0ng1a";
   wchar_t *string2 = L"strang1\x83";
#else
   #define LOCNAME "da_dk.ibm-1252"    /* Windows name */
   wchar_t *string1 = L"str\xE0ng1a";
   wchar_t *string2 = L"strang1\xE2";
#endif
int main(void)
{
   wchar_t *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 = wcsxfrm(NULL, string1, 0);
   pw1 = (wcslen(string1)/2);
   length2 = wcsxfrm(NULL, string2, 0);
   pw2 = (wcslen(string2)/2);
   if (NULL == (newstring1 = (wchar_t*)calloc(length1 + 1, sizeof(wchar_t))) ||
       NULL == (newstring2 = (wchar_t*)calloc(length2 + 1, sizeof(wchar_t)))) {
      printf("insufficient memory\n");
      exit(1);
   }
   /* Get primary weight of each string                                       */
   if ((wcsxfrm(newstring1, string1, pw1 + 1) != length1) ||
       (wcsxfrm(newstring2, string2, pw2 + 1) != length2)) {
      printf("error in string processing\n");
      exit(1);
   }
   if (0 != wcscmp(newstring1, newstring2))
      printf("wrong results\n");
   else
      printf("correct results\n");
   return 0;
   /***************************************************************************
      The output should be similar to:
      correct results
   ***************************************************************************/
}