您的位置:首页 > 教育 > 培训 > C语言之字符函数总结(全部!),一篇记住所有的字符函数

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

2025/2/24 8:44:26 来源:https://blog.csdn.net/x_p96484685433/article/details/139466438  浏览:    关键词:C语言之字符函数总结(全部!),一篇记住所有的字符函数


前言

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


字符分类函数和字符转换函数

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')

 以上函数共性:

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

字符转换函数

tolower

将大写字母转换为小写字母并返回

如果传入字符非大写字母,返回原传入字符

toupper

将小写字母转换为大写字母并返回

如果传入字符非小写字母,返回原传入字符

例如 tolower 函数

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

1:isalnum 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为字母或者数字
//(如:'a'~'z','A'~'Z','0'~'9')
int main()
{if (isalnum('a'))printf("是小写字母\n");if (isalnum('8'))printf("是数字\n");if (isalnum('Z'))printf("是大写字母\n");return 0;
}

运行结果:


2:isalpha 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为字母(如:'a'~'z','A'~'Z')
int main()
{if (isalpha('a'))printf("是字母\n");if (isalpha('B'))printf("是字母\n");if (isalpha('2') == 0)printf("不是字母\n");return 0;
}

运行结果:


3:isblank 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为,空格字符 ' ' 和水平制表符 '\t '这两种
int main()
{if (isblank(' '))printf("空格字符\n");if (isblank('\t'))printf("水平制表符\n");if (isblank('\n') == 0)printf("不认识\n");return 0;
}

运行结果:


4:iscntrl 函数

#include <stdio.h>
#include <ctype.h>int main()
{//判断字符是否为ASCII码值在0x00~0x1F之间,以及0x7F位置处的控制类字符//例子较多,只示例3个if (iscntrl('\n'))printf("true\n");if (iscntrl('\r'))printf("true\n");if (iscntrl(0x1F))printf("true\n");return 0;
}

运行结果:


5:isdigit 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为十进制数字(如:'0'~'9')
int main()
{if (isdigit('1'))printf("true\n");if (isdigit('9'))printf("true\n");if (isdigit(2) == 0)printf("false\n");return 0;
}

运行结果:


6:isgraph 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否具有图形表示(指的是所有可以打印出来的字符,
//也就是非空白字符和其他不可打印字符)
int main()
{int i = 0;//循环判断所有字符for (i = 0x0; i <= 0x7F; i++){if (isgraph('i'))printf("%c ", i);}return 0;
}

运行结果:


7:islower 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为小写字母
int main()
{int i = 0;for (i = 'a'; i <= 'z'; i++){if (islower(i)){printf("%c ", i);}}if (islower('A') == 0)printf("\nFalse");return 0;
}

运行结果:


8:isprint 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为可打印字符
int main()
{char c = 0;for (c = 32; c <= 126; c++){if (isprint(c)){printf("%c ", c);}}return 0;
}

运行结果:


9:ispunct 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为标点符号字符
int main()
{if (ispunct(','))printf("true\n");if (ispunct('.'))printf("true\n");if (ispunct('?'))printf("true\n");if (ispunct('a') == 0)printf("false\n");return 0;
}

运行结果:


10:isspace 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为空白字符
//(如: 空格' ',换页'\f',换行'\n',回车'\r',水平制表符'\t',垂直制表符'\v')
int main()
{if (isspace(' '))printf("true\n");if (isspace('\n'))printf("true\n");if (isspace('\t'))printf("true\n");if (isspace('\v'))printf("true\n");if (isspace('\f'))printf("true\n");if (isspace('\r'))printf("true\n");return 0;
}

运行结果:


11:isupper 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为大写字母
int main()
{int i = 0;for (i = 'A'; i <= 'Z'; i++){if (isupper(i)){printf("%c ", i);}}return 0;
}

运行结果:


12:isxdigit 函数

#include <stdio.h>
#include <ctype.h>//检查字符是否为16进制数字
int main()
{if (isxdigit('A'))printf("true\n");if (isxdigit('B'))printf("true\n");if (isxdigit('F'))printf("true\n");if (isxdigit('G') == 0)printf("false\n");return 0;
}

运行结果:


13:tolower 函数

#include <stdio.h>
#include <ctype.h>//将大写字母转换为小写字母并返回
int main()
{char c = 0;for (c = 'A'; c <= 'Z'; c++){printf("%c ", tolower(c));}printf("\n%c", tolower('A'));return 0;
}

运行结果:


14:toupper 函数

#include <stdio.h>
#include <ctype.h>//将小写字母转为大写字母并返回
int main()
{char c = 0;for (c = 'a'; c <= 'z'; c++){printf("%c ", toupper(c));}printf("\n%c", toupper('A'));return 0;
}

运行结果:


结语:

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com