来源:www.cncfan.com | 2006-4-28 | (有6839人读过)
1 字符测试函数
1> 函数原型均为int isxxxx(int) 2> 参数为int, 任何实参均被提升成整型 3> 只能正确处理处于[0, 127]之间的值
isalpha 'A'~'Z', 'a'~'z' isdigit '0'~'9' isxdigit '0'~'9', 'A'~'F', 'a'~'f' isalnum isalpha || isdigit islower 'a'~'z' isupper 'A'~'Z' isspace 9 '\t' (tab: 水平制表位) 10 '\n' (line feed: 换行) 11 '\v' (home: 垂直制表位) 12 '\f' (form feed: 换页) 13 '\r' (carriage return: 回车) 32 ' ' (space: 空格) isgraph 33~126 isprint isgraph || ' ' ispunct isgraph && !isalnum iscntrl 0~31, 127 2 字符映射函数
1> 函数原型为int toxxxx(int) 2> 对参数进行检测, 若符合范围则转换, 否则不变
int tolower(int); 'A'~'Z' ==> 'a'~'z' int toupper(int); 'a'~'z' ==> 'A'~'Z' 3 附测试代码
#include <stdio.h> #include <ctype.h>
char (* isxx_name[]) = { "isalpha", "isdigit", "isxdigit", "isalnum", "islower", "isupper", "isspace", "isgraph", "isprint", "ispunct", "iscntrl" };
int (* isxx_list[])(int) = { isalpha, isdigit, isxdigit, isalnum, islower, isupper, isspace, isgraph, isprint, ispunct, iscntrl };
int main() { int i, k; for (i=0; i<sizeof(isxx_list)/sizeof(isxx_list[0]); i++) { printf("Testing %s\n", isxx_name[i]); for (k=0; k<128; k++) if (isxx_list[i](k)) printf("%3d %c\n", k, k); printf("\n"); } return 0; }
source: 《C & C++ Code Capsules》
|