导航

cctype里的字符函数

Posted on 2009-12-17 15:05  spock  阅读(137)  评论(0)    收藏  举报
#include<iostream>  
#include<cctype>  
using namespace std;

void main()  
{  
    int n,i;  
    cout<<"输入文本,以@结束.\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<<"个字母"<<endl;
	cout<<whitespace<<"个标准空白字符"<<endl;
	cout<<digits<<"个数字"<<endl;
	cout<<punct<<"个标点"<<endl;
	cout<<others<<"个其他字符"<<endl;

}