[牛人杂谈] C语言字符分类函数

[复制链接]
 楼主| 捉虫天师 发表于 2016-11-9 21:24 | 显示全部楼层 |阅读模式
ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符、控制字符等等。既支持单字节(Byte)字符,也支持宽字符。
▪ isalpha
▪ iscntrl
▪ isdigit
▪ isgraph
▪ islower
▪ isupper
▪ tolower
▪ toupper
▪ isalnum
▪ isprint
▪ ispunct
▪ isspace
▪ isxdigit
▪ isascii

_______________________________
一共这么多,都怎么玩呢?
 楼主| 捉虫天师 发表于 2016-11-9 21:24 | 显示全部楼层
1 字符测试函数
1> 函数原型均为int isxxxx(int)
2> 参数为int, 任何实参均被提升成整型
3> 只能正确处理处于[0, 127]之间的值
2 字符映射函数
1> 函数原型为int toxxxx(int)
2> 对参数进行检测, 若符合范围则转换, 否则不变
int tolower(int); 'A'~'Z' ==> 'a'~'z'
int toupper(int); 'a'~'z' ==> 'A'~'Z'

 楼主| 捉虫天师 发表于 2016-11-9 21:25 | 显示全部楼层
isalpha
函数名称: isalpha
函数原型: int isalpha(char ch);
函数功能: 检查ch是否是字母.
函数返回: 是字母返回非0(在vs2015中为2) ,否则返回 0.
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int main()
  4. {
  5.     char ch1='*';
  6.     char ch2='a';
  7.     if(isalpha(ch1)!=0)
  8.         printf("%c is the ASCII alphabet\n",ch1);
  9.     else
  10.         printf("%c is not the ASCII alphabet\n",ch1);
  11.     if(isalnum(ch2)!=0)
  12.         printf("%c is the ASCII alphabet\n",ch2);
  13.     else
  14.         printf("%c is not the ASCII alphabet\n",ch2);
  15.     return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:27 | 显示全部楼层
iscntrl
函数名称: iscntrl
函数原型: int iscntrl(int ch);
函数功能: 检查ch是否控制字符(其ASCII码在0和0x1F之间,数值为 0-31).
函数返回: 是返回非0,否则返回 0.
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. char chars[]={'A',0x09,'Z'};
  4. #define SIZE sizeof(chars)/sizeof(char)
  5. int main()
  6. {
  7.     int i;
  8.     for(i=0;i<SIZE;i++)
  9.     {
  10.         printf("Char%cis%saControlcharacter\n",
  11.         chars[i],(iscntrl(chars[i]))?"":"not");
  12.     }
  13.     return 0;
  14. }



 楼主| 捉虫天师 发表于 2016-11-9 21:28 | 显示全部楼层
isdigit
函数名称: isdigit
函数原型: int isdigit(char ch);
函数功能: 检查ch是否是数字(0-9)
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int main()
  4. {
  5. char ch1='1';
  6. char ch2='a';
  7. if(isdigit(ch1)!=0)
  8. printf("%c is the ASCII number\n",ch1);
  9. else
  10. printf("%c is not the ASCII number\n",ch1);
  11. if(isdigit(ch2)!=0)
  12. printf("%c is the ASCII number\n",ch2);
  13. else
  14. printf("%c is not the ASCII number\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:29 | 显示全部楼层
isgraph
函数名称: isgraph
函数原型: int isgraph(int ch);
函数功能: 检查ch是否可显示字符(其ASCII码在0x21到0x7E之间),不包括空格
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int main()
  4. {
  5. charch1='';
  6. charch2='a';
  7. if(isgraph(ch1)!=0)
  8. printf("%cistheASCIIprintablecharacter\n",ch1);
  9. else
  10. printf("%cisnottheASCIIprintablecharacter\n",ch1);
  11. if(isgraph(ch2)!=0)
  12. printf("%cistheASCIIprintablecharacter\n",ch2);
  13. else
  14. printf("%cisnottheASCIIprintablecharacter\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:30 | 显示全部楼层
islower
函数名称: islower
函数原型: int islower(int ch);
函数功能: 检查ch是否小写字母(a-z)
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. charchars[]={'A','a','z','Z'};
  4. #defineSIZEsizeof(chars)/sizeof(char)
  5. int main()
  6. {
  7. int i;
  8. for(i=0;i<SIZE;i++){
  9. printf("Char%cis%salowercasecharacter\n",chars[i],(islower(chars[i]))?"":"not");
  10. }
  11. return0;
  12. }



 楼主| 捉虫天师 发表于 2016-11-9 21:42 | 显示全部楼层
isupper
函数名称: isupper
函数原型: int isupper(int ch);
函数功能: 检查ch是否是大写字母(A-Z)
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. charchars[]={'A','a','z','Z'};
  4. #defineSIZEsizeof(chars)/sizeof(char)
  5. int main()
  6. {
  7. inti;
  8. for(i=0;i<SIZE;i++){
  9. printf("Char%cis%sanuppercasecharacter\n",
  10. chars[i],(isupper(chars[i]))?"":"not");
  11. }
  12. return0;
  13. }



 楼主| 捉虫天师 发表于 2016-11-9 21:44 | 显示全部楼层
tolower
函数名称: tolower
函数原型: int tolower(int ch);
函数功能: 将ch字符转换为小写字母
函数返回: 返回ch所代表的字符的小写字母
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. intmain()
  5. {
  6. charx='a',y='b',z='A',w='*';
  7. printf("Character%ctoloweris%c\n",x,tolower(x));
  8. printf("Character%ctoloweris%c\n",y,tolower(y));
  9. printf("Character%ctoloweris%c\n",z,tolower(z));
  10. printf("Character%ctoloweris%c\n",w,tolower(w));
  11. return0;
  12. }



 楼主| 捉虫天师 发表于 2016-11-9 21:45 | 显示全部楼层
toupper
函数名称: toupper
函数原型: int toupper(int ch);
函数功能: 将ch字符转换成大写字母
函数返回: 与ch相应的大写字母
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. int main()
  5. {
  6. charx='a',y='b',z='A',w='*';
  7. printf("Character%ctoupperis%c\n",x,toupper(x));
  8. printf("Character%ctoupperis%c\n",y,toupper(y));
  9. printf("Character%ctoupperis%c\n",z,toupper(z));
  10. printf("Character%ctoupperis%c\n",w,toupper(w));
  11. return0;
  12. }



 楼主| 捉虫天师 发表于 2016-11-9 21:46 | 显示全部楼层
isalnum
函数名称: isalnum
函数原型: int isalnum(int ch);
函数功能: 检查ch是否是字母或数字
函数返回: 是字母或数字返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. intmain()
  4. {
  5. charch1='*';
  6. charch2='a';
  7. if(isalnum(ch1)!=0)
  8. printf("%cistheASCIInumberoralphebet\n",ch1);
  9. else
  10. printf("%cisnottheASCIInumbernoralphebet\n",ch1);
  11. if(isalnum(ch2)!=0)
  12. printf("%cistheASCIInumberoralphebet\n",ch2);
  13. else
  14. printf("%cisnottheASCIInumbernoralphebet\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:47 | 显示全部楼层
isprint
函数名称: isprint
函数原型: int isprint(int ch);
函数功能: 检查ch是否是可打印字符(包括空格),其ASCII码在0x20到0x7E之间
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. intmain()
  4. {
  5. charch1='\n';
  6. charch2='a';
  7. if(isprint(ch1)!=0)
  8. printf("%cistheASCIIprintablecharcater\n",ch1);
  9. else
  10. printf("%cisnottheASCIIprintablecharcater\n",ch1);
  11. if(isprint(ch2)!=0)
  12. printf("%cistheASCIIprintablecharcater\n",ch2);
  13. else
  14. printf("%cisnottheASCIIprintablecharcater\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:48 | 显示全部楼层
ispunct
函数名称: ispunct
函数原型: int ispunct(int ch);
函数功能: 检查ch是否是标点字符(不包括空格),即除字母,数字和空格以外的所有可打印字符
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. intmain()
  4. {
  5. charch1=',';
  6. charch2='a';
  7. if(ispunct(ch1)!=0)
  8. printf("%cistheASCIIpunct\n",ch1);
  9. else
  10. printf("%cisnottheASCIIpunct\n",ch1);
  11. if(ispunct(ch2)!=0)
  12. printf("%cistheASCIIpunct\n",ch2);
  13. else
  14. printf("%cisnottheASCIIpunct\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:49 | 显示全部楼层
isspace
函数名称: isspace
函数原型: int isspace(int ch);
函数功能: 检查ch是否是空格符和跳格符(控制字符)或换行符
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. intmain()
  4. {
  5. charch1='';
  6. charch2='a';
  7. if(isspace(ch1)!=0)
  8. printf("%cisthespacecharcater\n",ch1);
  9. else
  10. printf("%cisnotthespacecharcater\n",ch1);
  11. if(isspace(ch2)!=0)
  12. printf("%cisthespacecharcater\n",ch2);
  13. else
  14. printf("%cisnotthespacecharcater\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:50 | 显示全部楼层
isxdigit
函数名称: isxdigit
函数原型: int isxdigit(int ch);
函数功能: 检查ch是否是一个16进制数学字符(即0-9,或A-F,或a-f)
函数返回: 是返回非0,否则返回0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. intmain()
  4. {
  5. charch1='1';
  6. charch2='a';
  7. if(isxdigit(ch1)!=0)
  8. printf("%cistheASCIIhexadecimalnumber\n",ch1);
  9. else
  10. printf("%cisnottheASCIIhexadecimalnumber\n",ch1);
  11. if(isxdigit(ch2)!=0)
  12. printf("%cistheASCIIhexadecimalnumber\n",ch2);
  13. else
  14. printf("%cisnottheASCIIhexadecimalnumber\n",ch2);
  15. return0;
  16. }



 楼主| 捉虫天师 发表于 2016-11-9 21:51 | 显示全部楼层

isascii
函数名称: isascii
函数原型: int isascii(int ch)
函数功能: 测试参数是否是ASCII码0-127
函数返回: 是返回非0,否则返回0
参数说明: ch-被测参数

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. charchars[]={'A',0x80,'Z'};
  4. #defineSIZEsizeof(chars)/sizeof(char)
  5. intmain()
  6. {
  7. inti;
  8. for(i=0;i<SIZE;i++)
  9. {
  10. printf("Char%cis%sanASCIIcharacter\n",
  11. chars[i],(isascii(chars[i]))?"":"not");
  12. }
  13. return0;
  14. }


 楼主| 捉虫天师 发表于 2016-11-9 21:58 | 显示全部楼层
其实这些函数不仅仅C里面有,其他的语言也会用到,只要用到字符处理的地方都可以使用,比如在PHP语言就有。。

稳稳の幸福 发表于 2016-11-9 23:43 | 显示全部楼层
非常受用的例子,以前都是用到什么功能,自己写函数体,这会了就不用那么麻烦了。
mintspring 发表于 2016-11-10 19:04 | 显示全部楼层
用于大小写的转换和字符的判断识别,还是非常省事的。
zhuotuzi 发表于 2016-11-11 19:44 | 显示全部楼层
这个很好理解, is 就是是什么??
您需要登录后才可以回帖 登录 | 注册

本版积分规则

212

主题

3272

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部