使用cctype库对字符进行方便地判断
/*
cctype字符函数库的使用
*/
#include<iostream>
#include<cctype>
int main() {
using namespace std;
cout << "input 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);
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.\n";
return 0;
}
输出
input text for analysis, and type @ to terminate input.
i loved a girl 3 years ago, she called me hablee, and i called her yuki. when the days i graduate from master came,
she told me that i can't get a profitable work due to the delay graduation, so we should break up. i am disappointed about the relationship, finally just money issue. So i said, ok,let's break up, then we break up.
@
252 letters, 64 whitespace, 1 digits, 14 punctuations, 0 others.