wctype() function in C/C++

The wctype() is a built-in function in C/C++ which returns a value of type wctype_t that is used for classifying a wide character. It is defined within the cwctype header file of C++. The following are the possible type wctype_t:

Value of str Equivalent function
space iswspace
upper iswupper
xdigit iswxdigit
print iswprint
punct iswpunct
graph iswgraph
lower iswlower
cntrl iswcntrl
digit iswdigit
alpha iswalpha
blank iswblank
alnum iswalnum

Syntax:

wctype_t wctype(const char* str)

Parameter: The function accepts a single mandatory parameter str which specifies the desired wctype category.

Return Value: The function returns two values as shown below:

  • The function returns a wctype_t object that can be used with iswctype() ortowctype() to check a wide character’s property.
  • If str doesn’t provide a category supported by the current C locale, it returns zero.

Below programs illustrates the above function.
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    wchar_t wc = L'@';
  
    // checks if the type is digit
    if (iswctype(wc, wctype("digit")))
        wcout << wc << L" is a digit";
  
    // checks if the type is alpha
    else if (iswctype(wc, wctype("alpha")))
        wcout << wc << L" is an alphabet";
  
    else
        wcout << wc << L" is neither an"
              << " alphabet nor a digit";
  
    return 0;
}

chevron_right


Output:

@ is neither an alphabet nor a digit

Output:

@ is neither an alphabet nor a digit

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    wchar_t wc = L'g';
  
    // checks if the type is digit
    if (iswctype(wc, wctype("digit")))
        wcout << wc << L" is a digit";
  
    // checks if the type is alpha
    else if (iswctype(wc, wctype("alpha")))
        wcout << wc << L" is an alphabet";
  
    else
        wcout << wc << L" is neither"
              << " an alphabet nor a digit";
  
    return 0;
}

chevron_right


Output:

g is an alphabet


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.