实用字符处理函数库:cctype(ctype.h)

来源:互联网 发布:天祥网络 编辑:程序博客网 时间:2024/05/17 21:46
ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符、控制字符等等。既支持单字节(Byte)字符,也支持宽字符。

isalpha

函数名称: isalpha
函数原型: int isalpha(char ch);
函数功能: 检查ch是否是字母.
函数返回: 是字母返回非0(在vs2015中为2) ,否则返回 0.
参数说明:
所属文件 <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
int main()
{
    char ch1='*';
    char ch2='a';
    if(isalpha(ch1)!=0)
        printf("%c is the ASCII alphabet\n",ch1);
    else
        printf("%c is not the ASCII alphabet\n",ch1);
    if(isalnum(ch2)!=0)
        printf("%c is the ASCII alphabet\n",ch2);
    else
        printf("%c is not the ASCII alphabet\n",ch2);
    return0;
}

iscntrl

函数名称: iscntrl
函数原型: int iscntrl(int ch);
函数功能: 检查ch是否控制字符(其ASCII码在0和0x1F之间,数值为 0-31).
函数返回: 是返回非0,否则返回 0.
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<ctype.h>
char chars[]={'A',0x09,'Z'};
#define SIZE sizeof(chars)/sizeof(char)
int main()
{
    int i;
    for(i=0;i<SIZE;i++)
    {
        printf("Char%cis%saControlcharacter\n",
        chars[i],(iscntrl(chars[i]))?"":"not");
    }
    return 0;
}

isdigit

函数名称: isdigit
函数原型: int isdigit(char ch);
函数功能: 检查ch是否是数字(0-9)
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch1='1';
char ch2='a';
if(isdigit(ch1)!=0)
printf("%c is the ASCII number\n",ch1);
else
printf("%c is not the ASCII number\n",ch1);
if(isdigit(ch2)!=0)
printf("%c is the ASCII number\n",ch2);
else
printf("%c is not the ASCII number\n",ch2);
return0;
}
[1] 

isgraph

函数名称: isgraph
函数原型: int isgraph(int ch);
函数功能: 检查ch是否可显示字符(其ASCII码在0x21到0x7E之间),不包括空格
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
int main()
{
charch1='';
charch2='a';
if(isgraph(ch1)!=0)
printf("%cistheASCIIprintablecharacter\n",ch1);
else
printf("%cisnottheASCIIprintablecharacter\n",ch1);
if(isgraph(ch2)!=0)
printf("%cistheASCIIprintablecharacter\n",ch2);
else
printf("%cisnottheASCIIprintablecharacter\n",ch2);
return0;
}

islower

函数名称: islower
函数原型: int islower(int ch);
函数功能: 检查ch是否小写字母(a-z)
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<ctype.h>
charchars[]={'A','a','z','Z'};
#defineSIZEsizeof(chars)/sizeof(char)
int main()
{
int i;
for(i=0;i<SIZE;i++){
printf("Char%cis%salowercasecharacter\n",chars[i],(islower(chars[i]))?"":"not");
}
return0;
}

isupper

函数名称: isupper
函数原型: int isupper(int ch);
函数功能: 检查ch是否是大写字母(A-Z)
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<ctype.h>
charchars[]={'A','a','z','Z'};
#defineSIZEsizeof(chars)/sizeof(char)
int main()
{
inti;
for(i=0;i<SIZE;i++){
printf("Char%cis%sanuppercasecharacter\n",
chars[i],(isupper(chars[i]))?"":"not");
}
return0;
}

tolower

函数名称: tolower
函数原型: int tolower(int ch);
函数功能: 将ch字符转换为小写字母
函数返回: 返回ch所代表的字符的小写字母
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
intmain()
{
charx='a',y='b',z='A',w='*';
printf("Character%ctoloweris%c\n",x,tolower(x));
printf("Character%ctoloweris%c\n",y,tolower(y));
printf("Character%ctoloweris%c\n",z,tolower(z));
printf("Character%ctoloweris%c\n",w,tolower(w));
return0;
}

toupper

函数名称: toupper
函数原型: int toupper(int ch);
函数功能: 将ch字符转换成大写字母
函数返回: 与ch相应的大写字母
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
charx='a',y='b',z='A',w='*';
printf("Character%ctoupperis%c\n",x,toupper(x));
printf("Character%ctoupperis%c\n",y,toupper(y));
printf("Character%ctoupperis%c\n",z,toupper(z));
printf("Character%ctoupperis%c\n",w,toupper(w));
return0;
}

isalnum

函数名称: isalnum
函数原型: int isalnum(int ch);
函数功能: 检查ch是否是字母或数字
函数返回: 是字母或数字返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
intmain()
{
charch1='*';
charch2='a';
if(isalnum(ch1)!=0)
printf("%cistheASCIInumberoralphebet\n",ch1);
else
printf("%cisnottheASCIInumbernoralphebet\n",ch1);
if(isalnum(ch2)!=0)
printf("%cistheASCIInumberoralphebet\n",ch2);
else
printf("%cisnottheASCIInumbernoralphebet\n",ch2);
return0;
}

isprint

函数名称: isprint
函数原型: int isprint(int ch);
函数功能: 检查ch是否是可打印字符(包括空格),其ASCII码在0x20到0x7E之间
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
intmain()
{
charch1='\n';
charch2='a';
if(isprint(ch1)!=0)
printf("%cistheASCIIprintablecharcater\n",ch1);
else
printf("%cisnottheASCIIprintablecharcater\n",ch1);
if(isprint(ch2)!=0)
printf("%cistheASCIIprintablecharcater\n",ch2);
else
printf("%cisnottheASCIIprintablecharcater\n",ch2);
return0;
}

ispunct

函数名称: ispunct
函数原型: int ispunct(int ch);
函数功能: 检查ch是否是标点字符(不包括空格),即除字母,数字和空格以外的所有可打印字符
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
intmain()
{
charch1=',';
charch2='a';
if(ispunct(ch1)!=0)
printf("%cistheASCIIpunct\n",ch1);
else
printf("%cisnottheASCIIpunct\n",ch1);
if(ispunct(ch2)!=0)
printf("%cistheASCIIpunct\n",ch2);
else
printf("%cisnottheASCIIpunct\n",ch2);
return0;
}

isspace

函数名称: isspace
函数原型: int isspace(int ch);
函数功能: 检查ch是否是空格符和跳格符(控制字符)或换行符
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
intmain()
{
charch1='';
charch2='a';
if(isspace(ch1)!=0)
printf("%cisthespacecharcater\n",ch1);
else
printf("%cisnotthespacecharcater\n",ch1);
if(isspace(ch2)!=0)
printf("%cisthespacecharcater\n",ch2);
else
printf("%cisnotthespacecharcater\n",ch2);
return0;
}

isxdigit

函数名称: isxdigit
函数原型: int isxdigit(int ch);
函数功能: 检查ch是否是一个16进制数学字符(即0-9,或A-F,或a-f)
函数返回: 是返回非0,否则返回0
参数说明:
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<ctype.h>
intmain()
{
charch1='1';
charch2='a';
if(isxdigit(ch1)!=0)
printf("%cistheASCIIhexadecimalnumber\n",ch1);
else
printf("%cisnottheASCIIhexadecimalnumber\n",ch1);
if(isxdigit(ch2)!=0)
printf("%cistheASCIIhexadecimalnumber\n",ch2);
else
printf("%cisnottheASCIIhexadecimalnumber\n",ch2);
return0;
}

isascii

函数名称: isascii
函数原型: int isascii(int ch)
函数功能: 测试参数是否是ASCII码0-127
函数返回: 是返回非0,否则返回0
参数说明: ch-被测参数
所属文件: <ctype.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<ctype.h>
charchars[]={'A',0x80,'Z'};
#defineSIZEsizeof(chars)/sizeof(char)
intmain()
{
inti;
for(i=0;i<SIZE;i++)
{
printf("Char%cis%sanASCIIcharacter\n",
chars[i],(isascii(chars[i]))?"":"not");
}
return0;
}
0 0
原创粉丝点击