当前位置: C语言 -- 标准库 -- <wctype.h> -- wctype

wctype函数


概要:
#include <wctype.h>
wctype_t wctype(const char *property);

描述:

该函数构造一个wctype_t类型的值,该值描述了由字符串参数property表示的宽字符类别。

所有语言环境中字符串"alnum""alpha""blank""cntrl""digit""graph""lower""print""punct""space""upper""xdigit"都可以用作wctype函数的参数。


参数:
const char *property

表示宽字符类别的字符串,例如:"alnum""alpha""blank""cntrl""digit""graph""lower""print""punct""space""upper""xdigit"等。


返回值:

根据当前语言环境的LC_CTYPE类别,如果参数property表示有效的宽字符类别,函数返回非0值,该值可用作iswctype函数的第二个参数;否则函数返回0


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
/*函数wctype范例*/

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

void func(const char *property, const wchar_t *wStr)
{
    wctype_t desc;
    
    if(desc = wctype(property))
    {
        wprintf(L"%s是有效的宽字符类别。\n", property);
        
        int i = 0;
        int count = 0;    //计数器。

        while(wStr[i])
        {
            if(iswctype(wStr[i], desc))
                ++count;
            ++i;
        }

        wprintf(L"%ls中共%d个字母。\n", wStr, count);
    }
    else
        wprintf(L"%s不是有效的宽字符类别。\n", property);
}

int main(void)
{
    setlocale(LC_ALL, "");

    const wchar_t wStr[] = L"中国(China)";
    const char *propertyOne = "ALPHA";
    const char *propertyTwo = "alpha";

    func(propertyOne, wStr);
    func(propertyTwo, wStr);

    return 0;
}

输出:

ALPHA不是有效的宽字符类别。

alpha是有效的宽字符类别。

中国(China)中共7个字母。

注:使用ideone编译。


相关内容:
iswctype 检查宽字符分类类别的函数。