C library - wctrans() function



The C wctrans library wctrans() function is used to obtain a desc (descriptor) for a wctrans_t type. This descriptor can be used with the "iswctype" function to check whether a given wide character belongs to a specific character class.

This function can hold the following value of "str" −

  • toupper − identifies the mapping used by towupper
  • tolower − identifies the mapping used by towlower

Syntax

Following is the C library syntax of wctrans() function −

wctrans_t wctrans( const char* str )

Parameters

This function accepts a single parameter −

  • str − This is a pointer to a string of type "const char*" that specifies the name of the character class for which a descriptor is to be obtained.

Return Value

This function returns a object of type wctrans_t that can be used with the towctrans() function to map wide characters.

Example 1

Following is the basic c program to demonstrate the use of wctrans() to obtain a descriptor.

#include <wchar.h>
#include <wctype.h>
#include <stdio.h>

int main() {
   // Wide character to check
   wchar_t wc = L'A';
   
   // Descriptor for lowercase characters
   wctrans_t desc = wctrans("tolower");

   wprintf(L"Original character: %lc\n", wc);

   if (iswctype(wc, wctype("upper"))) {
      wc = towctrans(wc, desc);
      wprintf(L"Lowercase: %lc\n", wc);
   } else {
      wprintf(L"Not an uppercase character.\n");
   }
   return 0;
}

Output

Following is the output −

Original character: A
Lowercase: a

Example 2

Let's create another example, we use the wctrans() function to obtain a transformation descriptor and then use "towctrans" to transform lowercase characters to uppercase and vice versa.

#include <wctype.h>
#include <wchar.h>
#include <stdio.h>
#include <wctype.h>

int main() {
   wchar_t string[] = L"tUTORIALSPOINT.COM";
   wprintf(L"Original string: %ls \n", string);

   wctype_t lower= wctype("lower");
   wctype_t upper = wctype("upper");

   for (int i = 0; i < wcslen(string); i++) {
      // if charcter is in lower transform to upper
      if (iswctype(string[i], lower))
         string[i] = towctrans(string[i], wctrans("toupper"));
          
      // if charcter is in upper transform to lower
      else if (iswctype(string[i], upper))
         string[i] = towctrans(string[i], wctrans("tolower"));
   }

   wprintf(L"After transformation: %ls", string);
   return 0;
}

Output

Following is the output −

Original string: tUTORIALSPOINT.COM 
After transformation: Tutorialspoint.com

Example 3

This below example create a desc (descriptor) for "toupper". We then using this desc in wctrans() to transform to uppercase.

#include <wchar.h>
#include <wctype.h>
#include <stdio.h>

int main() {
   // Wide character string to check
   wchar_t str[] = L"tutorialspoint";
   // Descriptor for uppercase characters
   wctrans_t desc = wctrans("toupper");

   // Loop through each character in the string
   for (size_t i = 0; str[i] != L'\0'; ++i) {
      // Check if the character is alphanumeric
      if (iswctype(str[i], wctype("lower"))) {
         str[i] = towctrans(str[i], desc);
         wprintf(L"%lc ", str[i]);
      } else {
         wprintf(L"Not converted to uppercase");
      }
   }
   return 0;
}

Output

Following is the output −

T U T O R I A L S P O I N T 
c_library_wctype_h.htm
Advertisements