字符串分类统计C语言(1012)

题目描述

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

输入格式

一行字符,长度不超过200

样例输入

aklsjflj123 sadf918u324 asdf91u32oasdf/.';123

样例输出

23 16 2 4

题解一

#include<stdio.h>
int main()
{
    int digit = 0,number = 0,blank = 0,others = 0,c;      //分别为字母、数字、空格、其他
    while((c = getchar()) != '\n'){
        if(c >= 'A' && c<='Z' || c >= 'a' && c <= 'z')    //判断是否为字母
            number++;
        else if(c >= '0' && c <= '9')                     //判断是都为数字
            digit++;
        else if(c == ' ')                                 //判断是否为空格
            blank++;
        else                                              //其他
            others++;
    }
    printf("%d %d %d %d\n",number,digit,blank,others);
    return 0;
}

题解二

#include<stdio.h>
#include<ctype.h>    
//<ctype.h> 是一个与字符处理有关的头文件,它包含了一系列用于检测和转换单个字符的函数
//<ctype.h> 中的函数都只有一个参数,就是要检测或者要转换的字符,并且这个参数的类型是 int,
//它可以表示一个有效字符,或者一个EOF(无效字符)(本质上是-1)。
int main()
{   int number=0,digit=0,blank=0,others=0,c;
    while((c = getchar())!='\n'){    //定义int变量c,c得到字符的ascii码值
        if (isalpha(c)) number++;
        else if(isdigit(c)) digit++;
        else if(isblank(c))  blank++;
        else others++;
    }
	printf("%d %d %d %d",number,digit,blank,others);
	
	return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值