输入100个字符计算其中字母的个数C语言,【经典100题】题目17 输入一个字符串,分别统计出其中的英文字母,空格,数组,和其他字符的数量。...

C语言实现

#includevoid main()

{

int abc = 0;

int num = 0;

int space = 0;

int other = 0;

char str;

printf("请输入要统计的字符串");

while ((str=getchar())!='\n')

{

if ((str >= 'a'&&str <= 'z') || str >= 'A'&&str <= 'Z')

abc++;

else if (str == ' ')

space++;

else if (str >= '0'&&str <= '9')

num++;

else

other++;

}

printf("字母的数量:%d\n",abc);

printf("数字的数量:%d\n", num);

printf("空格的数量:%d\n", space);

printf("其他:%d\n", other);

}

运行结果:

请输入要统计的字符串the num 520 means I Love You @young people

字母的数量:30

数字的数量:3

空格的数量:8

其他:1

请按任意键继续. . .

python语言实现

str1 = str(input("请输入要统计的字符串:"))

abc = 0

num = 0

space = 0

other = 0

for i in range(0,len(str1)):

if str1[i] >="a" and str1[i] <= "z":

abc+=1

elif str1[i] >="A" and str1[i] <= "Z":

abc+=1

elif str1[i] >="0" and str1[i] <= "9":

num+=1

elif str1[i] == ' ':

space+=1

else:

other+=1

print("英文字母:%d"% abc)

print("数字:%d"% num)

print("空格:%d"% space)

print("其他:%d"% other)

'''

将ASCII字符转换为对应的数值即‘a’-->65,使用ord函数,ord('a')

反正,使用chr函数,将数值转换为对应的ASCII字符,chr(65)

'''

运行结果:

请输入要统计的字符串:the num 520 means I Love You @young people

英文字母:30

数字:3

空格:8

其他:1

★finished by songpl,2018.12.19

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值