ctype.h头文件(包含的函数)

前言

ctype.h

ctype.h 头文件是C标准库中的一个头文件,提供了一系列用于字符分类和转换的函数。这些函数主要用于处理单个字符,能够判断字符的类型(如字母、数字、空白字符等),以及将字符转换为大写或小写形式。

以下是 ctype.h 头文件中常用函数的介绍:

这些函数的参数应该是一个整数,通常是字符的ASCII码值,但它们也接受EOF(文件结束符)的值。这些函数返回非零值表示条件为真,否则返回0。

ctype.h 头文件中还可能包含了一些与本地化相关的函数,如 iswalnum()iswalpha(),它们用于宽字符的字符分类。这些函数与相应的ASCII版本的函数功能相同,但是操作的是宽字符(wchar_t)。

(注: 这些函数在处理输入时非常有用,特别是在需要对输入进行验证或者进行简单的字符操作时。)

一、isascii()函数:判断字符是否为ASCII码

#include<ctype.h>
#include<stdio.h>
int main()
{
   char ch;
    //输入字符
   ch=getchar();  
    //判断字符
   if(isascii(ch))
   {
      printf("%c is ascii.",ch);
   }
   else
   {
      printf("%c is not ascii.",ch);
   }
   putchar('\n');
   return 0;
}

注:判断字符是否为ASCII码,即字符ASCII在0到127之间

二、toascii()函数:把一个字符转换为ASCII码

#include<ctype.h>
 
#include<stdio.h>
 
int main()
{
 
   int ch1,ch2;
     //a(97) A(65)
   ch1='a'-32;
 
   ch2=toascii(ch1);
 
   printf("transform %c  to %c.\n",ch1,ch2);      
 
   return 0;  
 
}

注:把一个字符转换为ASCII,其实就是把八位二进制的最高变成0  

三、isalnum()函数:判断字符是否为字母或数字

#include<ctype.h>
#include<stdio.h>
int main(){
   char ch;
    //输入一个字符
   scanf("%c",&ch);
   if(isalnum(ch))
   {
      printf("%c is alnum.",ch);
   }
   else
   {
      printf("%c is not alnum.",ch);
   } 
   putchar('\n');
   return 0;  
}

注:判断字符是否为字母或数字

四、isalpha()函数:判断字符是否为英文字母

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
     //输入一个字符
   scanf("%c",&ch);
 
   if(isalpha(ch)){
 
      printf("%c is alpha.",ch);
 
   }else{
 
      printf("%c is not alpha.",ch);
 
   } 
     //putchar就是用来将一个字符(也可为转义字符) 数据输出(显示到屏幕的)
   putchar('\n');   
 
   return 0;  
 
}

五、isupper()函数:判断字符是否为大写英文字母

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(isupper(ch)){
 
      printf("%c is upper.",ch);
 
   }else{
 
      printf("%c is not uppper.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

六、islower()函数:判断字符是否为小写英文字母

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(islower(ch)){
 
      printf("%c is lower alpha.",ch);
 
   }else{
 
      printf("%c is not lower alpha.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

七、toupper()函数:把小写字母转换为大写字母

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
char ch1,ch2;
 
printf("input a character:");
 
scanf("%c",&ch1);
 
ch2=toupper(ch1);
 
printf("transform %c to %c.\n",ch1,ch2);
 
return 0;
 
}

八、tolower()函数:把大写字母转换为小写字母

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch1,ch2;
 
   printf("input a character:");
 
   scanf("%c",&ch1);
 
   ch2=tolower(ch1);
 
   printf("transform %c to %c.\n",ch1,ch2);
 
   return 0;  
 
}

九、isspace()函数:判断字符是否为空白字符

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(isspace(ch)){
 
      printf("%c is space.",ch);
 
   }else{
 
      printf("%c is not space.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

十、ispunct()函数:判断字符是否为标点符号

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(ispunct(ch)){
 
      printf("%c is punct.",ch);
 
   }else{
 
      printf("%c is not punct.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

十一、iscntrl()函数:判断字符是否为控制字符

#include<ctype.h>
 
#include<stdio.h>
 //提供控制台输入/输出
#include<conio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
     //从控制台获取字符条目
   ch=getche();
 
   if(iscntrl(ch)){
 
      printf("\n%c %d is control character.",ch,ch);
 
   }else{
 
      printf("\n%c %d is not control character.",ch,ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

十二、isdigit()函数:判断字符是否为十进制数字

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(isdigit(ch)){
 
      printf("%c is digit.",ch);
 
   }else{
 
      printf("%c is not digit.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

十三、isxdigit()函数:判断字符是否为十六进制字符

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(isxdigit(ch)){
 
      printf("%c is hex digit.",ch);
 
   }else{
 
      printf("%c is not hex digit.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

注:十六进制数字(0-9)(a-f)(A-F)

十四、isprint()函数:判断字符是否为可打印字符(含空格)

#include<ctype.h>
 
#include<stdio.h>
 
int main(){
 
   char ch;
 
   printf("input a character:");
 
   scanf("%c",&ch);
 
   if(isprint(ch)){
 
      printf("%c is print character.",ch);
 
   }else{
 
      printf("%c is not print character.",ch);
 
   } 
 
   putchar('\n');   
 
   return 0;  
 
}

十五、isgraph()函数:判断字符是否除空格外的可打印字符

#include<ctype.h>
#include<stdio.h>
int main(){
    char ch;
    printf("input a character:");
    scanf("%c",&ch);
    if(isgraph(ch)){
        printf("%c is graph.",ch);
    }else{
        printf("%c is not graph.",ch);
    }  
    putchar('\n');     
    return 0; 
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值