题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

def count_characters(s):
    # 初始化计数器
    count_letter = 0
    count_space = 0
    count_digit = 0
    count_other = 0

    # 遍历字符串中的每个字符,统计各类字符的个数
    for c in s:
        if c.isalpha():
            count_letter += 1
        elif c.isspace():
            count_space += 1
        elif c.isdigit():
            count_digit += 1
        else:
            count_other += 1

    # 返回统计结果
    return count_letter, count_space, count_digit, count_other

# 示例
s = str(input())
letter_count, space_count, digit_count, other_count = count_characters(s)
print(f"{letter_count}")
print(f"{space_count}")
print(f"{digit_count}")
print(f"{other_count}")

全部评论

相关推荐

03-11 21:46
西北大学 Java
河和静子:这只是实习工资,我学长北大通班博一的,他同学被这家天天发邮件让他去实习,一个月10w
点赞 评论 收藏
分享
野猪不是猪🐗:现在的环境就是这样,供远大于求。 以前卡学历,现在最高学历不够卡了,还要卡第一学历。 还是不够筛,于是还要求得有实习、不能有gap等等... 可能这个岗位总共就一个hc,筛到最后还是有十几个人满足这些要求。他们都非常优秀,各方面都很棒。 那没办法了,看那个顺眼选哪个呗。 很残酷,也很现实
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务