C语言之字符函数总结(全部!),一篇记住全部的字符函数 ...

打印 上一主题 下一主题

主题 954|帖子 954|积分 2872





前言

        还在担心关于字符的库函数记不住吗?不用担心,这篇文章将为你全面整理全部的字符函数的用法。不用影象,一次看完,随查随用。用多了自然就记住了




字符分类函数和字符转换函数
   C语言中有一系列的函数是专门做字符分类和字符转换的,也就是一个字符是属于什么类型的字符的,以及将字符转换为大写或小写,这些函数的使用都需要包含⼀个头头件是<type.h>
  字符分类函数:
函数函数判断为真返回非0值,否则返回0
isalnum
检查字符是否为字母或者数字
(如:'a'~'z','A'~'Z','0'~'9')
isalpha
检查字符是否为字母(如:'a'~'z','A'~'Z')
isblank检查字符是否为空格字符 ' ' 和水平制表符 '\t '这两种
iscntrl
检查字符是否为控制字符,指那些通常用于控制设备,不显示在屏幕上的字符
(如:ASCII码值在0x00~0x1F之间的字符,以及0x7F位置处的字符)
isdigit
检查字符是否为十进制数字(如:'0'~'9')
isgraph
检查字符是否具有图形表示(指的是全部可以打印出来的字符,
也就是非空白字符和其他不可打印字符)
islower
检查字符是否为小写字母
isprint
检查字符是否可打印
(ASCII范围通常为 (空格)32~126(~) 之间)
ispunct
检查字符是否为标点符号字符
isspace 检查字符是否为空白字符
(如: 空格' ',换页'\f',换行'\n',回车'\r',水平制表符'\t',垂直制表符'\v')
isupper 检查字符是否为大写字母
(如:'A'~'Z)
isxdigit
检查字符是否为十六进制数字
(如:'A'~'F')
 以上函数共性:

  • 形参都为 int c,函数返回类型都为 int (注:字符也属于整形类),如下图


字符转换函数
tolower
将大写字母转换为小写字母并返回
假如传入字符非大写字母,返回原传入字符
toupper
将小写字母转换为大写字母并返回
假如传入字符非小写字母,返回原传入字符
例如 tolower 函数



接下来我将演示这些函数的用法:
1:isalnum 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为字母或者数字
  4. //(如:'a'~'z','A'~'Z','0'~'9')
  5. int main()
  6. {
  7.         if (isalnum('a'))
  8.                 printf("是小写字母\n");
  9.         if (isalnum('8'))
  10.                 printf("是数字\n");
  11.        
  12.         if (isalnum('Z'))
  13.                 printf("是大写字母\n");
  14.         return 0;
  15. }
复制代码
运行效果:




2:isalpha 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为字母(如:'a'~'z','A'~'Z')
  4. int main()
  5. {
  6.         if (isalpha('a'))
  7.                 printf("是字母\n");
  8.         if (isalpha('B'))
  9.                 printf("是字母\n");
  10.         if (isalpha('2') == 0)
  11.                 printf("不是字母\n");
  12.         return 0;
  13. }
复制代码
运行效果:




3:isblank 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为,空格字符 ' ' 和水平制表符 '\t '这两种
  4. int main()
  5. {
  6.         if (isblank(' '))
  7.                 printf("空格字符\n");
  8.         if (isblank('\t'))
  9.                 printf("水平制表符\n");
  10.         if (isblank('\n') == 0)
  11.                 printf("不认识\n");
  12.         return 0;
  13. }
复制代码
运行效果:




4:iscntrl 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main()
  4. {
  5.         //判断字符是否为ASCII码值在0x00~0x1F之间,以及0x7F位置处的控制类字符
  6.         //例子较多,只示例3个
  7.         if (iscntrl('\n'))
  8.                 printf("true\n");
  9.         if (iscntrl('\r'))
  10.                 printf("true\n");
  11.         if (iscntrl(0x1F))
  12.                 printf("true\n");
  13.         return 0;
  14. }
复制代码
运行效果:




5:isdigit 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为十进制数字(如:'0'~'9')
  4. int main()
  5. {
  6.         if (isdigit('1'))
  7.                 printf("true\n");
  8.         if (isdigit('9'))
  9.                 printf("true\n");
  10.         if (isdigit(2) == 0)
  11.                 printf("false\n");
  12.         return 0;
  13. }
复制代码
运行效果:




6:isgraph 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否具有图形表示(指的是所有可以打印出来的字符,
  4. //也就是非空白字符和其他不可打印字符)
  5. int main()
  6. {
  7.         int i = 0;
  8.     //循环判断所有字符
  9.         for (i = 0x0; i <= 0x7F; i++)
  10.         {
  11.                 if (isgraph('i'))
  12.                         printf("%c ", i);
  13.         }
  14.         return 0;
  15. }
复制代码
运行效果:




7:islower 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为小写字母
  4. int main()
  5. {
  6.         int i = 0;
  7.         for (i = 'a'; i <= 'z'; i++)
  8.         {
  9.                 if (islower(i))
  10.                 {
  11.                         printf("%c ", i);
  12.                 }
  13.         }
  14.         if (islower('A') == 0)
  15.                 printf("\nFalse");
  16.         return 0;
  17. }
复制代码
运行效果:




8:isprint 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为可打印字符
  4. int main()
  5. {
  6.         char c = 0;
  7.         for (c = 32; c <= 126; c++)
  8.         {
  9.                 if (isprint(c))
  10.                 {
  11.                         printf("%c ", c);
  12.                 }
  13.         }
  14.         return 0;
  15. }
复制代码
运行效果:




9:ispunct 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为标点符号字符
  4. int main()
  5. {
  6.         if (ispunct(','))
  7.                 printf("true\n");
  8.         if (ispunct('.'))
  9.                 printf("true\n");
  10.         if (ispunct('?'))
  11.                 printf("true\n");
  12.         if (ispunct('a') == 0)
  13.                 printf("false\n");
  14.         return 0;
  15. }
复制代码
运行效果:




10:isspace 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为空白字符
  4. //(如: 空格' ',换页'\f',换行'\n',回车'\r',水平制表符'\t',垂直制表符'\v')
  5. int main()
  6. {
  7.         if (isspace(' '))
  8.                 printf("true\n");
  9.         if (isspace('\n'))
  10.                 printf("true\n");
  11.         if (isspace('\t'))
  12.                 printf("true\n");
  13.         if (isspace('\v'))
  14.                 printf("true\n");
  15.         if (isspace('\f'))
  16.                 printf("true\n");
  17.         if (isspace('\r'))
  18.                 printf("true\n");
  19.         return 0;
  20. }
复制代码
运行效果:




11:isupper 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为大写字母
  4. int main()
  5. {
  6.         int i = 0;
  7.         for (i = 'A'; i <= 'Z'; i++)
  8.         {
  9.                 if (isupper(i))
  10.                 {
  11.                         printf("%c ", i);
  12.                 }
  13.         }
  14.         return 0;
  15. }
复制代码
运行效果:




12:isxdigit 函数

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //检查字符是否为16进制数字
  4. int main()
  5. {
  6.         if (isxdigit('A'))
  7.                 printf("true\n");
  8.         if (isxdigit('B'))
  9.                 printf("true\n");
  10.         if (isxdigit('F'))
  11.                 printf("true\n");
  12.         if (isxdigit('G') == 0)
  13.                 printf("false\n");
  14.         return 0;
  15. }
复制代码
运行效果:




13:tolower 函数
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //将大写字母转换为小写字母并返回
  4. int main()
  5. {
  6.         char c = 0;
  7.         for (c = 'A'; c <= 'Z'; c++)
  8.         {
  9.                 printf("%c ", tolower(c));
  10.         }
  11.         printf("\n%c", tolower('A'));
  12.         return 0;
  13. }
复制代码
运行效果:




14:toupper 函数
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. //将小写字母转为大写字母并返回
  4. int main()
  5. {
  6.         char c = 0;
  7.         for (c = 'a'; c <= 'z'; c++)
  8.         {
  9.                 printf("%c ", toupper(c));
  10.         }
  11.         printf("\n%c", toupper('A'));
  12.         return 0;
  13. }
复制代码
运行效果:




结语:

        一开始准备和字符串函数一起写,写到下面发现篇幅过长了,字符串函数放在下一篇来讲,末了感谢各人的支持。



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表