【问题描述】
输入一行文本,分别统计出其中英文字母、空格、整数数字和其他字符的个数。
【输入形式】
输入一行文本。
【输出形式】
共四行,分别输出该行文本中的英文字母、空格、整数数字和其他字符的个数。
【样例输入】
6t 4er;9.3e r 2
【样例输出】
alpha:5
space:4
digit:5
other:2
【样例说明】
这行文本中有英文字母:t、e、r、e、r,共5个;
这行文本中有空格:t后面1个,e后面1个,r后面2个,共4个;
这行文本中有数字:6、4、9、3、2,共5个;
这行文本中有其他字符:分号;和圆点.,共2个。
#include<iostream> using namespace std; int main() { char ch; int alpha=0,space=0,digit=0,other=0; while((ch=cin.get())!='\n') //cin.get()表示输入包括数字,字母,空格,回车...;!=‘\n’为不是换行 { if(ch>='0'&&ch<='9') digit++; else if(ch>='a'&&ch<='z') alpha++; else if(ch>='A'&&ch<='Z') alpha++; else if(ch==' ') space++; else other++; } cout<<"alpha:"<<alpha<<endl; cout<<"space:"<<space<<endl; cout<<"digit:"<<digit<<endl; cout<<"other:"<<other; return 0; }