题解 | #统计字符#

统计字符

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}")

全部评论

相关推荐

07-04 16:00
门头沟学院 Java
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务