题解 | #统计字符#
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
分类累计,主要是依据ascii范围ord()进行分类。 需要记住ascii表的范围:
数字: 48~57;
大写字母:75~90;
小写字母:97~122;
空格就直接用字符‘ ’来对比;
while True:
try:
s = input()
Total_c = 0
Total_s = 0
Total_n = 0
Total_o = 0
for i in s:
if 75<=ord(i)<=90 or 97<=ord(i)<=122:
Total_c+=1
elif 48<=ord(i)<=57:
Total_n+=1
elif i == ' ':
Total_s+=1
else:
Total_o+=1
print(Total_c)
print(Total_s)
print(Total_n)
print(Total_o)
except:
break