【C语言】处理字符的库函数

判断字符的库函数

iscntrl()函数

简介

  • 查询c字符是否为控制字符

  • 控制字符:是指不占据显示器上打印位置的字符

函数声明

int iscntrl ( int c );

返回值和返回类型

  • 是控制字符则返非零
  • 不是控制字符返回零

使用案例

#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="first line \n second line \n";
  while (!iscntrl(str[i]))
  {
    putchar (str[i]);
    i++;
  }
  return 0;

输出:

image-20220729153103779

isspace()函数

简介

  • 判断是否为空白字符
  • 空白字符:空格‘ ’,换页‘\f’,换行’\n’,回车‘\r’,制表符’\t’或者垂直制表符’\v’

函数声明

int isspace ( int c );

返回值和返回类型

  • 是空白字符返回非零
  • 非空白字符返回零

使用案例

#include <stdio.h>
#include <ctype.h>
int main ()
{
  char c;
  int i=0;
  char str[]="Example sentence to test isspace\n";
  while (str[i])
  {
    c=str[i];
    if (isspace(c)) c='\n';
    putchar (c);
    i++;
  }
  return 0;
}

输出:

image-20220729153540823

isdigit()函数

简介

  • 判断是否为十进制数字

函数声明

int isdigit ( int c );

返回值和返回类型

  • 是十进制数字则返回非零
  • 非十进制数字则返回零

使用案例

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
  char str[]="1776ad";
  int year;
  if (isdigit(str[0]))
  {
    year = atoi (str);
    printf ("The year that followed %d was %d.\n",year,year+1);
  }
  return 0;
}

输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RqgIUHU8-1659109152375)(http://rf7goskp0.hn-bkt.clouddn.com/image-20220729154817898.png)]

isxdigit()函数

简介

  • 判断字符是否为16进制数字

函数声明

int isxdigit ( int c );

返回值和返回类型

  • 是十六进制数字则返回非零
  • 非十六进制数字则返回零

islower()函数

简介

  • 判断字符是否为小写

函数声明

int islower ( int c );

返回值和返回类型

  • 是小写则返回非零
  • 非小写则返回零

isupper()函数

简介

  • 判断是否为大小写

函数声明

int isupper ( int c );

返回类型和返回值

  • 是大写则返回非零
  • 非大写则返回零

isalpha()函数

简介

  • 判断是否为字母

函数声明

int isalpha ( int c );

返回值和返回类型

  • 是字母则返回非零
  • 非字母则返回零

isalnum()函数

简介

  • 判断是否为字母或者是十进制数字

函数声明

int isalnum ( int c );

返回值和返回类型

  • 是字母或者十进制数字则返回非零
  • 非字母或者十进制数字则返回零

ispunct ()函数

简介

  • 判断字符是否为标点符号

函数声明

int ispunct ( int c );

返回值和返回类型

  • 是标点符号则返回非零
  • 非标点符号则返回零

isgraph ()函数

简介

  • 判断是否有图形标识的字符

函数声明

int isgraph ( int c );

返回值和返回类型

  • 有图形标识的字符则返回非零
  • 无图形标识的字符则返回零

使用案例

#include <stdio.h>
#include <ctype.h>
int main ()
{
  FILE * pFile;
  int c;
  pFile=fopen ("myfile.txt","r");
  if (pFile)
  {
    do {
      c = fgetc (pFile);
      if (isgraph(c)) putchar (c);
    } while (c != EOF);
    fclose (pFile);
  }
}

转换字符大小写的库函数

tolower()函数

简介

  • 将大写字母转换为小写字母

函数声明

int tolower ( int c );

返回值和返回类型

  • 返回对应小写的ascii值
  • 如果是小写则不变

toupper()函数

简介

  • 将小写字母转换为大写字母

函数声明

int toupper ( int c );

返回值和返回类型

  • 返回对应大写的ascii值
  • 如果是大写则不变
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值