题目描述
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
输入
一行字符
输出
统计值
样例输入
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
样例输出
23 16 2 4
#include <iostream>
using namespace std;
int main()
{
char ch;
int word=0,space=0,number=0,others=0;//一定要赋初值为0,因为默认值不一定就为0
while(ch!='\n')//当等于回车(/n)时结束输入
{
ch=getchar();//getchar()读取一个字符
if(('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
{
word++;
}
else if(ch==' ')
{
space++;
}
else if('0'<=ch&&ch<='9')
{
number++;
}
else
{
others++;
}
}
cout<<word<<" "<<number<<" "<<space<<" "<<others-1<<" "<<endl;//others要减去1是因为最后一个回车也被读入了
return 0;
}