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。 |
我演示第一个和最后一个,比较有代表性。
- #include <iostream>
- #include <cctype>
- using namespace std;
- int main()
- {
- char c;
- c='d';
- if(isalnum(c))
- {
- cout<<c<<" is an alpha or number";
- }
- else
- cout<<c<<" is an not alpha or number";
- }
输出结果如下图:
- #include <iostream>
- #include <cctype>
- using namespace std;
- int main()
- {
- char c;
- c='D';
- cout<<char(tolower(c));
- }
tolower()函数返回的是一个整数类型,所以如果不重载这个函数的话就通过类型转换来实现正确的输出,注意看哦,输出结果如下: