#include < cctype>

一、官方解释

Character handling functions
This header declares a set of functions to classify and transform individual characters.

Functions
These functions take the int equivalent of one character as parameter and return an int that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.

二、关于头文件

ctype定义了可以对单个字符进行分类和转换的相关函数。字符对应有相应的编码,函数的参数是字符参数的等价int编码,返回一个int值。返回值可以用于表示其他字符,也可以看做是bool值,0表示false,非0表示true。

Note:C语言中,用0表示“逻辑假”,用非0数表示“逻辑真”。C++中沿用了C的习惯,但增加了布尔类型变量(bool),布尔类型变量用true表示真,用false表示假。

三、库函数

int isalnum(int c);

1、作用:检查字符是否为字母数字。如果isalpha函数或isdigit函数也返回true,则结果为true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是字母、数字,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "ab2c.. -+";for(int i=0; i<9; i++){if(isalnum(str[i])){cout << "str[" << i << "] is alphabetic or number" << endl;}}return 0;
}

输出:
str[0] is alphabetic or number
str[1] is alphabetic or number
str[2] is alphabetic or number
str[3] is alphabetic or number

int isalpha(int c);

1、作用:检查字符是否为字母。如果是字母则返回true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是字母,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "ab2c.. -+";for(int i=0; i<9; i++){if(isalpha(str[i])){cout << "str[" << i << "] is alphabetic" << endl;}}return 0;
}

输出:
str[0] is alphabetic
str[1] is alphabetic
str[3] is alphabetic

int isblank(int c);

1、作用:检查参数c是否为空格。如果是空格,则返回true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是空,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "ab2c.. -+";for(int i=0; i<9; i++){if(isblank(str[i])){cout << "str[" << i << "] is blank" << endl;}}return 0;
}

输出:
str[6] is blank

int iscntrl(int c);

1、作用:检查参数c是否为控制符。如果是控制符,则返回true。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是控制符,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "ab2c..\n-+";for(int i=0; i<9; i++){if(iscntrl(str[i])){cout << "str[" << i << "] is control character" << endl;}}return 0;
}

输出:
str[6] is control character

int isdigit(int c);

1、作用:检查参数c是否为十进制数字。如果是十进制,则返回true
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是十进制数值,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str1 = "123ab";string str2 = "ab123";int num = 1;cout << isdigit(str1[0]);cout << isdigit(str2[0]);cout << isdigit(num);return 0;
}

输出:
1 0 0

int isgraph(int c);

1、作用:检查参数c是否有图形表示。
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c有图形表示,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
图形化表示的字符是除了空格字符(’ ')之外的所有可以打印的字符(由isprint确定)。
3、实例

#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);}
}
}

Note:借官方文档一用

int islower(int c);

1、作用:检查字符是否是小写字符字母
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是小写,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "123abABC";int i = 0;while(i<8){cout << islower(str[i++]) ;}return 0;
}

输出:(笔者用的DEV C++)
00022000

int isprint(int c);

1、作用:检查参数c是否是可以输出的
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是可以输出的,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
可打印字符是在显示中占据打印位置的字符。ASCII字符集中,可打印字符的ASCII码大于0x1f (US),小于0x7f (DEL)。
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "first line \n second line \n";int i=0;while(i<27){if(isprint(str[i])){putchar(str[i]);}i++;}return 0;
}

输出:first line second line

int ispunct(int c);

1、作用:检查参数c是否是标点符号
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是标点符号,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
在c++中,该函数的特定于语言环境的模板版本(ispunct)存在于头文件locale.h中。

3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "Hello, world! Hello C";int cnt = 0;int i = 0;while(str[i]){if(ispunct(str[i++]))cnt++;}cout << cnt << " punctuation characters in total" << endl;return 0;
}

输出:2 punctuation characters in total

int isspace(int c);

1、作用:检查参数c是否是空白符
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是空白符,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
空白符包括:

字符 ASCII 含义
’ ’ 0x20 空格(SPC)
’ \t ’ 0x09 制表符(TAB)
’ \n ’ 0x0a 换行(LF)
’ \v ’ 0x0b 垂直制表符(VT)
’ \f ’ 0x0c 换页符(FF)
’ \r ’ 0x0d 回车(CR)

3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "Hello +\n World   hello C hello C++ ";int i = 0;while(str[i]){if(isspace(str[i]))putchar('1');else putchar('0');i++;}cout << '\n' << str << endl;return 0;
}

输出:
00000101100000111000001010000010001
Hello +
World hello C hello C++

int isupper(int c);

1、作用:检查参数c是否是大写字母
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是大写,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "123abABC";int i = 0;while(i<8){cout << isupper(str[i++]) ;}return 0;
}

输出:
00000111

int isxdigit(int c);

1、作用:检查参数c是否为十六进制数字
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是十六进制,返回非0值(true);否则返回0(false)
(3)参数:被检查的字符
3、实例:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;
int main(void)
{string str = "16taf";int i = 0;while(str[i]){if(isxdigit(str[i]))putchar(str[i]);i++;}return 0;
}

输出:
16af
注:十六进制数是1、2、3、4、5、6、7、8、9、a、b、c、d、e、f

int tolower(int c);

1、作用:把大写字母转小写
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是大写字母,返回对应的小写字母
(3)参数:要转换的大写字母
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "Hello World 123";int i = 0;while(str[i]){putchar(tolower(str[i++]));}return 0;
}

输出:
hello world 123

int toupper(int c);

1、作用:把小写字母转大写
2、用法:
(1)调用ctype.h,C++可写#include < cctype >
(2)返回值:如果c是小写字母,返回对应的大写字母
(3)参数:要转换的小写字母
3、实例:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main(void)
{string str = "Hello World 123";int i = 0;while(str[i]){putchar(toupper(str[i++]));}return 0;
}

输出:
HELLO WORLD 123

C/C++函数库 之 ctype.h相关推荐

  1. 2020-11-02C 标准库 - <ctype.h>

    C 标准库 - <ctype.h> 简介 C 标准库的 ctype.h 头文件提供了一些函数,可用于测试和映射字符. 这些函数接受 int 作为参数,它的值必须是 EOF 或表示为一个无符 ...

  2. C语言 : 标准库 - <ctype.h>

    简介 C 标准库的 ctype.h 头文件提供了一些函数,可用于测试和映射字符. 这些函数接受 int 作为参数,它的值必须是 EOF 或表示为一个无符号字符. 如果参数 c 满足描述的条件,则这些函 ...

  3. C语言数学函数库<math.h>及常用函数

    C语言数学函数库<math.h>及常用函数 一.<math.h> C语言中常用的一个数学函数库,里面涵盖了常用的数学运算,如求对数.指数.绝对值.三角函数.两数中最大数等.使用 ...

  4. C语言库文件ctype.h中重要的库函数

    C Primer Plus第七章分支跳转章节中,提到了ctype.h头文件中的一些用于判断字符类型的库函数接口. isalnum() 字母数字 isalpha() 字母 isblank() 标准的空白 ...

  5. c语言图形函数linerel,C语言图形函数库总结graphics.h

    囊括了graphics.h库的主要用法及说明 C语言图形.图像函数库graphics.h.txt C语言图形.图像函数库graphics.h (一) 像素函数 56. putpiel() 画像素点函数 ...

  6. Turbo c 函数库中文说明

    Turbo c 函数库中文说明 分类函数 所在函数库为ctype.h  int isalpha(int ch) 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0  int isal ...

  7. C 标准库 - ctype.h

    C 标准库 - <ctype.h> 简介 C 标准库的 ctype.h 头文件提供了一些函数,可用于测试和映射字符. 这些函数接受 int 作为参数,它的值必须是 EOF 或表示为一个无符 ...

  8. linux下w5500驱动程序,W5500驱动函数库.pdf

    W5500驱动函数库 W5500 驱动函数库 /**************************************************************************** ...

  9. C语言再学习 -- ctype.h字符判断函数

    参看:ctype.h 百度百科 ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特 ...

  10. 头文件 ctype.h 以及函数 isalpha() tolower()

    ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符.控制字 ...

最新文章

  1. 2019年9月全国程序员工资统计。
  2. vs2010给c语言文件添加头注释
  3. 配置yum源并与公网同步更新
  4. valueOf与toString方法研究
  5. tcount在哪个文件里_在cad中tcount快速编号命令怎么用,求教
  6. Count Complete Tree Nodes
  7. 【转载】OmniGraffle (四)化繁为简
  8. 将某个网站嵌入到iframe的测试
  9. c 复杂的前置后置面试题_OPPO Reno拆解:优秀工艺由外而内,复杂用料不负旗舰之名...
  10. dsp实现快速傅里叶的C语言程序,DSP-快速傅立叶变换(FFT)算法实验
  11. C# 实现酒店房态图
  12. 灯泡亮度控制单片机_如何有效保护投影机灯泡 保护投影机灯泡方法【详解】...
  13. java中的tcp与多线程_Java5 多线程与TCP编程实践
  14. JAVA笔记--数组和字符串常用方法
  15. 端口镜像--锐捷交换机
  16. SQL Server Always Encrypted加密使用
  17. 网易上线短视频创作平台“网易知识公路“
  18. netcat使用总结
  19. 轻开B2C电子商务网站(20141231版)发布手册
  20. 嵌入式开发(7)系统定时器(SysTick)之延时函数运用

热门文章

  1. 指南针c语言程序,HMC5883L电子指南针单片机程序
  2. 我们为什么存在于三维空间而不是四维空间
  3. c语言建立循环链表,C语言实现循环链表
  4. 分支定界法求解旅行商问题
  5. 第2章 业务:数据驱动运营
  6. 语音助手——未来趋势
  7. 牛头刨床机构动力学的分析(C语言建模)
  8. 摸索Detours 1:使用Vs2019 编译Detours
  9. Python学习路线(课程大纲+Python视频教程+下载地址)
  10. java计算机毕业设计水果商城设计MyBatis+系统+LW文档+源码+调试部署