#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; 
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐