看一个对cctype中主要方法的介绍的例子:
#include "iostream"
#include "cctype"
using namespace std;
int main() {
cout << "Enter text for analysis, and type @ to terminate input " << endl;
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
cin.get(ch);
while (ch != '@')
{
if (isalpha(ch)) {
chars++;
} else if(isspace(ch)) {
whitespace++;
} else if(isdigit(ch)) {
digits++;
} else if(ispunct(ch)) {
punct++;
} else {
others++;
}
cin.get(ch);
}
cout << chars << " letters, "
<< whitespace << " whitespace, "
<< digits << " digits, "
<< punct << " punctuations, "
<< others << " others." << endl;
return 0;
}
程序运行结果为:
以下是cctype里主要的函数及作用:
isalnum() 判断传入的参数是否是字母或者数字, 是返回true, 否则false
isalpha() 判断是否是字母
iscntrl() 判断是否是控制字符
isdigit() 判断是否是0~9的数字
isgraph() 是否是出空格之外的打印字符
islower() 是否是小写字母
isprint() 是否是打印字符(包含空格)
ispunct() 是否是标点符号
isspace() 是否是空白字符, 如:空格, 换行符, 回车, 制表符等
isupper() 是否是大写字母
isxdigit() 是否是十六进制数字
tolower() 转化为小写字符
toupper() 转化为大写字符