ctype.h 介绍
头文件 ctype.h
是标准 C 库的一部分。它提供了分类和转换字符的功能。
所有这些函数都有一个参数,一个必须是 EOF 或者可以表示为 unsigned char 的 int
。
分类函数的名称以 is
为前缀。如果传递给它的字符满足相关条件,则每个返回一个整数非零值(TRUE)。如果条件不满足,则函数返回零值(FALSE)。
假设默认的 C 语言环境,这些分类函数如图所示运行:
int a;
int c = 'A';
a = isalpha(c); /* Checks if c is alphabetic (A-Z, a-z), returns non-zero here. */
a = isalnum(c); /* Checks if c is alphanumeric (A-Z, a-z, 0-9), returns non-zero here. */
a = iscntrl(c); /* Checks is c is a control character (0x00-0x1F, 0x7F), returns zero here. */
a = isdigit(c); /* Checks if c is a digit (0-9), returns zero here. */
a = isgraph(c); /* Checks if c has a graphical representation (any printing character except space), returns non-zero here. */
a = islower(c); /* Checks if c is a lower-case letter (a-z), returns zero here. */
a = isprint(c); /* Checks if c is any printable character (including space), returns non-zero here. */
a = isupper(c); /* Checks if c is a upper-case letter (a-z), returns zero here. */
a = ispunct(c); /* Checks if c is a punctuation character, returns zero here. */
a = isspace(c); /* Checks if c is a white-space character, returns zero here. */
a = isupper(c); /* Checks if c is an upper-case letter (A-Z), returns non-zero here. */
a = isxdigit(c); /* Checks if c is a hexadecimal digit (A-F, a-f, 0-9), returns non-zero here. */
Version >= C99
a = isblank(c); /* Checks if c is a blank character (space or tab), returns non-zero here. */
有两种转换功能。这些是使用前缀’to’命名的。这些函数采用与上述相同的参数。但是,返回值不是简单的零或非零,但传递的参数以某种方式更改。
假设默认的 C 语言环境,这些转换函数如图所示运行:
int a;
int c = 'A';
/* Converts c to a lower-case letter (a-z).
* If conversion is not possible the unchanged value is returned.
* Returns 'a' here.
*/
a = tolower(c);
/* Converts c to an upper-case letter (A-Z).
* If conversion is not possible the unchanged value is returned.
* Returns 'A' here.
*/
a = toupper(c);
以下信息引自 cplusplus.com 映射每个分类类型函数如何考虑原始 127 个字符的 ASCII 集(a•表示该函数对该字符返回非零值)
ASCII 值 | 字符 | iscntrl 判断 | ISBLANK | isspace 为 | isupper | islower 判断 | 因而 isalpha | ISDIGIT | isxdigit 判断 | 字符 isalnum | ispunct 判断 | isgraph | isprint 判断 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0x00 .. 0x08 | NUL,(其他控制代码) | • | |||||||||||
×09 | 标签(’\ t’) | • | • | • | |||||||||
0x0A .. 0x0D | (空格控制代码:’\ f’,’\ v’,’\ n’,’\ r’) | • | • | ||||||||||
0x0E .. 0x1F | (其他控制代码) | • | |||||||||||
为 0x20 | 空间 (’ ‘) | • | • | • | |||||||||
0x21 .. 0x2F | !“#$%&’()* +, - 。/ | • | • | • | |||||||||
0x30 .. 0x39 | 0123456789 | • | • | • | • | • | |||||||
0x3a .. 0x40 | :; <=> @ | • | • | • | |||||||||
0x41 .. 0x46 | ABCDEF | • | • | • | • | • | • | ||||||
0x47 .. 0x5A | GHIJKLMNOPQRSTUVWXYZ | • | • | • | • | • | |||||||
0x5B .. 0x60 | [] ^ _` | • | • | • | |||||||||
0x61 .. 0x66 | ABCDEF | • | • | • | • | • | • | ||||||
0x67 .. 0x7A | ghijklmnopqrstuvwxyz | • | • | • | • | • | |||||||
0x7B .. 0x7E | {}〜bar | • | • | • | |||||||||
0x7F |
(DEL) | • |