cctype 头文件定义函数实例
cctype 头文件所包含的函数主要用来测试字符值,以下是随便找的一个表,但是对于初学者来说,怎么用呢,自己上机操作解决,后两个返回的是int型,确实很意外,强制转换一下,很简单。
isalnum(c) | 假如c是字母或数字,则为true |
isalpah(c) | 假如c是字母,则为true |
iscntrl(c) | 假如c是控制字符,则为true |
isdigit(c) | 假如c是数字,则为true |
isgraph(c) | 假如c不是空格,则为true |
islower(c) | 假如c是小写字母,则为true |
isprint(c) | 假如c是可打印的字符,则为true |
ispunct(c) | 假如c是标点符号,则为true |
isspace(c) | 假如c是空白字符,则为true |
isupper(c) | 假如c是大写字母,则为true |
isxdigit(c) | 假如c是十六进制数,则为true |
tolower(c) | 假如c是大写字母,则返回小写字母形式,否则返回c。 |
toupper(c) | 假如c是小写字母,则返回大些字母形式,否则返回c。 |
我演示第一个和最后一个,比较有代表性。
1 #include <iostream>
2 #include <cctype>
3 using namespace std;
4 int main()
5 {
6 char c;
7 c='d';
8 if(isalnum(c))
9 {
10 cout<<c<<" is an alpha or number";
11 }
12 else
13 cout<<c<<" is an not alpha or number";
14
15
16 }
输出结果如下图:
tolower()函数返回的是一个整数类型,所以如果不重载这个函数的话就通过类型转换来实现正确的输出,注意看哦,1 #include <iostream>
2 #include <cctype>
3 using namespace std;
4 int main()
5 {
6 char c;
7 c='D';
8 cout<<char(tolower(c));
9
10 }输出结果如下: