C基础-统计字符

题目描述
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

样例输入
a 1,
样例输出
1
1
1
1

示例代码

#include <iostream>
using namespace std;
int main(){
  char a[100]={NULL};
  int i=0;
  int num=0;
  int space=0;
  int letter=0;
  int other=0;
  cin.getline(a,100);
  while(a[i]){
    if(a[i]>='0' && a[i]<='9') num++;
    else if(( a[i]>='a' && a[i]<='z')||( a[i]>='A' &&a[i]<='Z')) letter++;
    else if(a[i]==' ')space++;
    else other++;
    i++;
  }
  cout<<letter<<endl;
  cout<<space<<endl;
  cout<<num<<endl;
  cout<<other<<endl;
  return 0;
}

猜你喜欢

转载自blog.csdn.net/three_cats/article/details/90049430