当前位置: 代码迷 >> 综合 >> 字符的统计(C语言)
  详细解决方案

字符的统计(C语言)

热度:64   发布时间:2023-11-02 08:34:24.0
题目描述
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
输入
输出
样例输入
a 1,
样例输出
1
1
1
1
#include <stdio.h>
#include<string.h>int main() {char str[128];          //定义字符串int  alphabet=0,space=0,number=0,other=0;gets(str);             //字符串的输入int i=0;for(int i=0;i<strlen(str);i++){       //strlen()求字符串的长度if((str[i]>='a' && str[i]<='z') ||(str[i])>='A' && str[i]<='Z'){alphabet++;}else if(str[i]==' '){space++;} else if(str[i]>='0' && str[i]<='9'){     //因为是char型所以加''number++;}else{other++;}}printf("%d\n",alphabet);printf("%d\n",space);printf("%d\n",number);printf("%d",other);return 0;
}

 

  相关解决方案