题解 | #统计字符#

统计字符

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

全部评论

相关推荐

在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
孙艹肘:校招不给三方直接让实习我都去了,,主打一个在学校呆着也是闲着,不如出来实习一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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