C library - wctype() function



The C wctype library wctype() function is used to obtain a desc (descriptor) for a wide character 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" −

  • alnum − identifies the category used by iswalnum
  • alpha − identifies the category used by iswalpha
  • blank − identifies the category used by iswblank (C99)
  • cntrl − identifies the category used by iswcntrl
  • digit − identifies the category used by iswdigit
  • graph − identifies the category used by iswgraph
  • lower − identifies the category used by iswlower
  • print − identifies the category used by iswprint
  • space − identifies the category used by iswspace
  • upper − identifies the category used by iswupper
  • xdigit − identifies the category used by iswxdigit

Syntax

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

wctype_t wctype( 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 value of type wctype_t which is the descriptor for the specified character class.

Example 1

Following is the basic c program to demonstrate the use of wctype() 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 alphabetic characters
   wctype_t desc = wctype("alpha");
   
   // Use iswctype to check if the character has the property described by desc
   if (iswctype(wc, desc)) {
      wprintf(L"%lc is an alphabetic character.\n", wc);
   } else {
      wprintf(L"%lc is not an alphabetic character.\n", wc);
   }
   
   return 0;
}

Output

Following is the output −

A is an alphabetic character.

Example 2

Let's create another c example and use the wctype() to create a desc (descriptor) of an alphanumeric character.

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

int main() {
   // Wide character string to check
   wchar_t str[] = L"tutorialspoint12";
   // Descriptor for alphanumeric characters
   wctype_t desc = wctype("alnum");

   // 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], desc)) {
         wprintf(L"%lc is an alphanumeric character.\n", str[i]);
      } else {
         wprintf(L"%lc is not an alphanumeric character.\n", str[i]);
      }
   }
   return 0;
}

Output

Following is the output −

t is an alphanumeric character.
u is an alphanumeric character.
t is an alphanumeric character.
o is an alphanumeric character.
r is an alphanumeric character.
i is an alphanumeric character.
a is an alphanumeric character.
l is an alphanumeric character.
s is an alphanumeric character.
p is an alphanumeric character.
o is an alphanumeric character.
i is an alphanumeric character.
n is an alphanumeric character.
t is an alphanumeric character.
1 is an alphanumeric character.
2 is an alphanumeric character.

Example 3

This below example create a desc (descriptor) for a punctuation character. we then check whether wc is a punctuation character.

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

int main() 
{ 
   wchar_t wc = L'#';
   // checks if the character is a punctuation
   if (iswctype(wc, wctype("punct"))) 
      wprintf(L"%lc is a punctuation\n", wc); 
   else
      wprintf(L"%lc is not a punctuation\n", wc);
   return 0; 
}

Output

Following is the output −

# is a punctuation
c_library_wctype_h.htm
Advertisements