The iswctype
function in C is used to check whether or not a given wide character has a particular property. For instance, we can use the iswctype
function to check if a wide character is in lower or upper case.
To access the iswctype
function and declare a wide character string of type wchar_t
, the wctype.h
and wchar.h
libraries must be included in the program, respectively.
#include <wctype.h>
#include <wchar.h>
int iswctype(wint_t character, wctype_t prop);
The iswctype
function in C takes in two parameters:
character
– The wide character that needs to be checked.prop
– The property that the wide character needs to be checked for. The data-type of the property parameter is wctype_t
. The value in the property variable is generated using the wctype
function, which takes in a string denoting a particular property. The wctype_t
data-type and the wctype
function can be accessed through the wctype.h
library.The wctype
can take in many types of strings as its argument. Some of these are shown in the table below:
String | Property |
---|---|
lower | lowercase |
upper | uppercase |
alpha | alphanumeric |
printable |
The iswctype
function returns either 0 or any non-zero number, which denotes the presence or absence of a particular property, respectively.
The code snippet below shows how we can use the iswctype
function to check if a particular wide character is in lower case or upper case.
We declare a sample string and create a variable of type wctype_t
using the wctype
function. The string “upper” is passed to the wctype
function as its parameter, which returns the required value of type wctype_t
. This value and each wide character of the sample string is passed to the iswctype
function iteratively in a while
loop as its arguments. The return value of the iswctype
function is checked in an if-else
statement, and the program prints the property of each character on the console! We can see this here:
/* iswctype example */#include <stdio.h>#include <wctype.h>#include <wchar.h>int main (){int x = 0;wchar_t sample_string[] = L"EDUCative\n";wctype_t check = wctype("upper");while ( sample_string[x] ){if ( iswctype(sample_string[x],check) ){printf("Upper Case\n");}else{printf("Lower Case\n");}x++;}return 0;}
Free Resources