#include<iostream>
#include<cctype>
using namespace std;
int main()
{
int chars=0;
int spaces=0;
int digit=0;
int punct=0;
int others=0;
char ch;
cout<<"Enter text for analysis,and type @ to terminate.\n";
cin.get(ch);
while(ch!='@')
{
if(isalpha(ch))
chars++;
else if(isspace(ch))
spaces++;
else if(isdigit(ch))
digit++;
else if(ispunct(ch))
punct++;
else
others++;
cin.get(ch);
}
cout<<chars<<"letters "<<spaces<<"whitespace "<<digit<<"digit "<<punct<<"punct "<<others<<"others.\n";
return 0;
}
注意 isalpha(),isdigit(),isspace()等函数的应用。
还有就是单个字符是如何循环读取的。