c语言字符串各类字符的个数,c语言统计字符串中各个字符的个数

51987418d3c61812238c7a734a0597e6.png

目标:

输入一行字符,统计其中各种字符的个数。

具体代码:#include

#include

#include

#define M 1024

void main() {

char str[M];

fgets(str, M, stdin);

int space = 0;

int letter = 0;

int num = 0;

int other = 0;

for (int i = 0; i < (int)strlen(str); ++i) {

if (str[i] == ' ') {

space += 1;

}

else if (str[i] > 64 && str[i] < 91 || str[i]>96 && str[i] < 123) {

letter += 1;

}

else if (str[i] > 47 && str[i] < 58) {

num += 1;

}

else {

if (str[i] != '

') {//因为fgets()函数会在末尾自动加上

,影响判断结果,需要判断是否为换行符

other += 1;

}

}

}

printf("空格的个数为:%d

", space);

printf("英文字母的个数为:%d

", letter);

printf("数字的个数为:%d

", num);

printf("其他字符的个数为:%d

", other);

system("pause");

}

注意:fgets()函数会在字符串末尾( 前)读入我们在键盘上敲的回车即换行符

运行结果如下:

4ef589244c7d731613ff2dc2860126e7.png

推荐教程:c语言教程

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值