cctype字符函数库使用

#include <iostream>
#include <cctype>     //字符函数原型头文件 
using namespace std;

int main()
{
	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 != '#')            // test for sentinel
	{
		if(isalpha(ch))         // 字符
			chars++;
		else if(isspace(ch))    // 标准空白字符,如空格、进纸、换行符、回车、水平制表符或者垂直制表符
			whitespace++;
		else if(isdigit(ch))    // 数字(0~9)
			digits++;
		else if(ispunct(ch))    // 标点符号
			punct++;
		else
			others++;
		cin.get(ch);            // get next character
	}
	cout << chars << " letters, "
		<< whitespace << " whitespace, "
		<< digits << " digits, "
		<< punct << " punctuations, "
		<< others << " others.\n";
	return 0; 
}

你可能感兴趣的:(C++,标准,库)