- C 标准库
- C 库 - 首页
- C 库 - <assert.h>
- C 库 - <complex.h>
- C 库 - <ctype.h>
- C 库 - <errno.h>
- C 库 - <fenv.h>
- C 库 - <float.h>
- C 库 - <inttypes.h>
- C 库 - <iso646.h>
- C 库 - <limits.h>
- C 库 - <locale.h>
- C 库 - <math.h>
- C 库 - <setjmp.h>
- C 库 - <signal.h>
- C 库 - <stdalign.h>
- C 库 - <stdarg.h>
- C 库 - <stdbool.h>
- C 库 - <stddef.h>
- C 库 - <stdio.h>
- C 库 - <stdlib.h>
- C 库 - <string.h>
- C 库 - <tgmath.h>
- C 库 - <time.h>
- C 库 - <wctype.h>
- C 标准库资源
- C 库 - 快速指南
- C 库 - 有用资源
- C 库 - 讨论
C 库 - wctype() 函数
C 的wctype库wctype()函数用于获取宽字符类型的描述符(desc)。此描述符可与“iswctype”函数一起使用,以检查给定的宽字符是否属于特定的字符类别。
此函数可以保存以下“str”值:
- alnum - 识别 iswalnum 使用的类别
- alpha - 识别 iswalpha 使用的类别
- blank - 识别 iswblank (C99) 使用的类别
- cntrl - 识别 iswcntrl 使用的类别
- digit - 识别 iswdigit 使用的类别
- graph - 识别 iswgraph 使用的类别
- lower - 识别 iswlower 使用的类别
- print - 识别 iswprint 使用的类别
- space - 识别 iswspace 使用的类别
- upper - 识别 iswupper 使用的类别
- xdigit - 识别 iswxdigit 使用的类别
语法
以下是wctype()函数的 C 库语法:
wctype_t wctype( const char* str );
参数
此函数接受一个参数:
-
str - 这是一个指向类型为“const char*”的字符串的指针,它指定要获取其描述符的字符类别的名称。
返回值
此函数返回类型为 wctype_t 的值,它是指定字符类别的描述符。
示例 1
以下是用wctype()获取描述符的基本 C 程序示例。
#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;
}
输出
以下是输出:
A is an alphabetic character.
示例 2
让我们再创建一个 C 示例,并使用wctype()创建字母数字字符的描述符(desc)。
#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;
}
输出
以下是输出:
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.
示例 3
以下示例创建标点字符的描述符(desc)。然后我们检查 wc 是否为标点字符。
#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;
}
输出
以下是输出:
# is a punctuation
c_library_wctype_h.htm
广告