华为机试HJ40统计字符

千行

关注

阅读 27

2024-07-24

华为机试HJ40统计字符

题目:

在这里插入图片描述

想法:

统计上述题目中的四种字符的个数存入字典中,按指定顺序进行输出

input_str = input()

str_dict = {"alpha": 0, "space": 0, "number": 0, "others": 0}

for i in input_str:
    if i.isalpha():
        str_dict["alpha"] += 1
    elif i == " ":
        str_dict["space"] += 1
    elif i.isdigit():
        str_dict["number"] += 1
    else:
        str_dict["others"] += 1

print(str_dict["alpha"])
print(str_dict["space"])
print(str_dict["number"])
print(str_dict["others"])

精彩评论(0)

0 0 举报