1200: 字符串数字字母空格其他字符的个数

题目描述

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

输入

一行字符。

输出

分别输出这行字符中的英文字母、空格、数字和其他字符的个数,用空格隔开。
请注意行尾输出换行。

样例输入

What are you doing? 123456

样例输出

15 4 6 1

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     char str[1000];
 5     fgets(str,1000,stdin);
 6     int a=0,b=0,c=0,d=0;
 7     int len=strlen(str);
 8     for(int i=0;i<len-1;i++){
 9         if((str[i]>='A'&&str[i]<='Z' )||( str[i]>='a'&&str[i]<='z')){
10             a++;
11         }else if(str[i]==' '){
12             b++;
13         }else if(str[i]>='0'&&str[i]<='9'){
14             c++;
15         }else{
16             d++;
17         }
18     }
19     printf("%d %d %d %d\n",a,b,c,d);
20     return 0;
21 }

Mist Note:没啥说的,主要是通过这个例子发现fgets函数好像会把换行符读进去。当你在dos窗口按enter,回车也会被收进去。

注意去除换行符。

posted on 2019-01-30 10:18  Aldrich_2020  阅读(354)  评论(0编辑  收藏  举报