HJ40 统计字符
思路:
step1:输入字符,同时创建“shuzi”,将0-9数字添加进去,同时设置4个计数器;
step2:对n进行遍历,如果i是字母,count1 += 1;如果i是空格,count2 += 1;如果i在“shuzi”中,count3 += 1;如果是其他字符,count4 += 1;
分别打印四个计数器
代码如下:
n = input()
shuzi = '0123456789'
count1 = 0
count2 = 0
count3 = 0
count4 = 0
for i in n:
if i.isalpha():
count1 += 1
elif i == ' ':
count2 += 1
elif i in shuzi:
count3 += 1
else:
count4 += 1
print(count1)
print(count2)
print(count3)
print(count4)