【问题描述】
从键盘输入n个字符,请分别统计大写字母、小写字母、数字、其他字符的个数并输出;还需要输出所有数字字符之和。
【输入形式】
第一行为一个整数n(100>=n>=0),接下来n行每行一个字符。
【输出形式】
输出第1行为4个整数,分别表示大写字母、小写字母、数字、其他字符的个数,第2行为一个数字,表示其中所有数字字符所对应的数字之和,当输入的字符中不包含数字字符时,没有第2行。
【样例输入1】
5 a A 5 6 @
【样例输出1】
1 1 2 1 11
【样例输入2】
5 a A B Z !
【样例输出2】
3 1 0 1
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,sum,up=0,low,digit,other;
char a;
cin>>n;
for (int i=0; i<n; i++)
{
cin>>a;
if (isupper(a)) //isupper、islower、isdigit来自头文件cmath,用于判断
{
up+=1;
}
else if (islower(a))
{
low+=1;
}
else if (isdigit(a))
{
digit+=1;
sum+=a-'0'; //char a是以ASCII码的形式出现的如果是数字,要减去0的码值
}
else
{
other+=1;
}
}
cout<<up<<" "<<low<<" "<<digit<<" "<<other<<endl;
if (digit!=0)
{
cout<<sum;
}
}