统计字母数字等个数

Problem Description

输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符)

Input

多组测试数据,每行一组

Output

每组输出一行,分别是字母个数,数字个数,空格字数以及其他字符个数

Sample Input

I am a student in class 1.
I think I can!

Sample Output

18 1 6 1
10 0 3 1




源代码如下:

#include <iostream>
#include<cctype>
using namespace std;

 char s[100];
int main()
{
    int alpha,num,space,other;
   while(gets(s))
   {
       alpha=0,num=0,space=0,other=0;
       for(int i=0;s[i]!='';++i)
       {
           if(isalpha(s[i]))      ++alpha;
           else if(isdigit(s[i])) ++num;
           else if(s[i]==32)      ++space;
           else                   ++other;
       }
       cout<<alpha<<" "<<num<<" "<<space<<" "<<other<<endl;
   }
    return 0;
}


原文地址:https://www.cnblogs.com/yldf/p/6249938.html