<ctype.h>是c标准库里的头文件,提供了一些用于测试和映射字符的函数,下面将介绍其中的一些函数。
1.isascii( )函数
isascii是c语言中的字符检测函数,通常用于检测字符c是否为ASCII码字符;
参数类型:int
返回值:若为ASCII码字符返回true(非0),否则返回null(0);
#include<stdio.h>
#include<ctype.h>
int main()
{
for(int i=125;i<=130;i++)
{
if(isascii(i)!=0)
{
printf("%d是ascall码字符\n",i);
}
else
{
printf("%d不是ascall码字符\n",i);
}
}
}
运行结果:
2,isalpha()函数
isalpha( )判断字符是否为 英文字母
参数:int
返回值:若为英文字母,返回true(非0),否则返回0;
#include<stdio.h>
#include<ctype.h>
int main()
{
printf("%d\n",isalpha('a'));
printf("%d\n",isalpha('A'));
printf("%d\n",isalpha('9'));
return 0;
}
运行结果:
3. isdigit()
isdigit()函数用于判断字符是否为阿拉伯数字
参数:int
返回值:若为阿拉伯数字,返回非0整数,否则返回0;
#include <stdio.h>
#include <ctype.h>
int main()
{
char a = 'c',b = '1';
printf("%d\n",isdigit(a));
printf("%d\n",isdigit(b));
return 0;
}
运行结果:
4.isalnum()
isalnum函数用于判断字符是否为字母或数字;
参数:int
返回值:若为字母或数字,返回非0整数,否则返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
char a = 'c', b = '2', c = '@';
printf("%d\n",isalnum(a));
printf("%d\n",isalnum(b));
printf("%d\n",isalnum(c));
return 0;
}
运行结果:
5.isblank( )
isblank()函数用于检查字符是否为空白字符('space' || 'tab');
参数:int
返回值:如果为空白字符,返回非0整数,否则返回0;
#include <stdio.h>
#include <ctype.h>
int main()
{
char a = 'c', b = '3', c = ' ';
printf("%d",isblank(a));
printf("%d",isblank(b));
printf("%d",isblank(c));
return 0;
}
运行结果:
6,isspace()
它用来检查参数c是否为空白字符。也就是判断是否为空格(' ')、水平定位字符('\t')、归位键('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
参数:int
返回值:如果为空白字符,返回非0整数,否则返回0;
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d\n",isspace(' '));
printf("%d\n",isspace(' '));
printf("%d\n",isspace('9'));
}
运行结果:
7、islower()
这个函数用来检查参数c是否为小写英文字母。
参数:int
返回值:如果为空白字符,返回非0整数,否则返回0;
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d\n",islower('a'));
printf("%d\n",islower('A'));
printf("%d\n",islower('9'));
return 0;
}
运行结果:
8、isupper()
此函数用来判断字符c是否为大写英文字母,
参数:int
返回值:如果为空白字符,返回非0整数,否则返回0;
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d\n",isupper('a'));
printf("%d\n",isupper('A'));
printf("%d\n",isupper('9'));
return 0;
}
运行结果:
9、toupper( )
这个函数用来将字符c转换为大写英文字母;
参数:int
返回值:返回对应的大写字母
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%c\n",toupper('a'));
printf("%c\n",toupper('b'));
printf("%c\n",toupper('A'));
printf("%c\n",toupper('9'));
return 0;
}
运行结果:
10、tolower()
此函数用来 将字符c转换为小写英文字母;
参数:int
返回值:返回对应的小写字母
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%c\n",tolower('a'));
printf("%c\n",tolower('b'));
printf("%c\n",tolower('A'));
printf("%c\n",tolower('B'));
printf("%c\n",tolower('9'));
return 0;
}
运行结果: