@[TOC]
<ctype.h> 测试和映射字符
isprint 检测是否可打印
空格也可以打印
int isprint(int ASCII)
可打印返回非零
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if (isprint(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
输出
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
isgraph 检测是否有图形表示(空格除外)
int isgraph(int ASCII)
可打印返回非零(空格除外)
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if (isgraph(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
isalnum 检测是否是数字或者字母
int isalnum(int ASCII)
检测你的ASCII是不是数字或者字母
1不是ASCII的1
‘1’才是ASCII的‘1’
函数
返回非零状态,表示字母数字
返回零,不是字母数字
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if (isalnum(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
输出
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
isalpha 检测是不是字母
int isalpha(int ASCII)
是字母返回非零
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if (isalpha(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
输出
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
islower 检测是小写字母
int islower(int ASCII)
是小写字母返回非零
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if ( islower(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
输出
a b c d e f g h i j k l m n o p q r s t u v w x y z
isupper 检测是大写字母
int isupper(int c)
是大写字母返回非零
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if ( isupper(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
输出
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
isdigit 检测是不是10以内的ASCII->10进制数字
int isdigit(inr ASCII)
是10进制数字返回非零
否者返回0
‘0x20’不是10进制数字,更像一个字符串
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if ( isdigit(i)!=0)
printf("%c ",i);
}
puts("");
return 0;
}
输出
0 1 2 3 4 5 6 7 8 9
isxdigit 检测是不是16进制的字符串
是16进制数字返回非零
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
int i=0;
char str[64]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(i=0;i<64;i++)
{
if (isxdigit(str[i])!=0)
printf("%c ",str[i]);
}
puts("");
return 0;
}
输出
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
iscntrl 检测是不是控制字符
int iscntrl(int ASCII)
是控制字符返回非零
否者返回0
控制字符不可打印
#include <stdio.h>
#include <ctype.h>
int main()
{
for (int i=0;i<=127;++i)
{
if (iscntrl(i)!=0)
printf("%d ",i);
}
puts("");
return 0;
}
输出它的10进制
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127
ispunct 检测是不是标点符号
int ispunct(int ascii)
可标点符号返回非零(空格除外)
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
int i;
printf("C 语言中所有可打印的标点符号: \n");
for (i=0;i<=127;++i)
{
if (ispunct(i)!=0)
printf("%c ",i);
}
return 0;
}
输出
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
isspace 检测是不是空白字符
int isspace(int c)
空白字符:
' ' (0x20) space (SPC) 空格符
'\t' (0x09) horizontal tab (TAB) 水平制表符
'\n' (0x0a) newline (LF) 换行符
'\v' (0x0b) vertical tab (VT) 垂直制表符
'\f' (0x0c) feed (FF) 换页符
'\r' (0x0d) carriage return (CR) 回车符
是返回非零
否者返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
int i;
int ecx=0;
for (i=0;i<=127;++i)
{
if (isspace(i)!=0)
ecx++;
}
printf("0-127中,有%d个空白字符",ecx);
return 0;
}
输出
0-127中,有6个空白字符
tolower 大写变小写
int tolower(int 大写)
返回值是小写的值
如果原本本来就是小写,或者他不是字母就不处理
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char str[] = "DQX is FINE";
while( str[i] )
{
putchar(tolower(str[i]));
i++;
}
puts("");
return(0);
}
输出
dqx is fine
toupper 小写变大写
int toupper(int c)
返回一个大写的int值
如果原来就是大写,或者字符不存在大写就不管
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char str[] = "dqx IS fien";
while( str[i] )
{
putchar(toupper(str[i]));
i++;
}
puts("");
return(0);
}