#include <iostream>
#include <cctype>
int main()
{
using namespace std;
cout<<"Enter text for analysis, and type @"
"to terminate input.\n";
char ch;
int whitespace =0;
int digits=0;
int chars=0;
int punct=0;
int others=0;
cin.get(ch);// get first character
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 "
<<others<<" others.\n";
return 0;
}
C++从C继承的一个与字符相关的、非常方便的函数软件包,它可以简化注入确定字符是否为大写字母、数字、标点符号等工作。
函数的原型是在头文件cctype中定义的。
该头文件中的字符函数有:
isalnum() 如果参数是字母数字,即字母或数字,该函数返回true
isalpha() 如果参数是字母,该函数返回true
iscntrl() 如果参数是控制字符,返回true
isdigit() 如果参数哦是数字(0-9),返回true
isgraph() 参数是除空格之外的打印字符,true
islower() 判断是否小写字母
isupper() 判断是否大写字母
isprint() 判断是否打印字符(包括空格)
ispunct() 判断是否标点符号
isspace() 判断是否是标准空白字符,如空格、进纸、换行符、回车、水平制表符或者垂直制表符、
isxdigit() 判断是否为十六进制数字0-9,a-f,A-F
tolower() 参数大写,返回小写,否则返回原参数
toupper() 小写转大写