输入一个字符串,分别统计字符串中字母,数字,空格和其字符的个数用代码怎么表示
时间: 2023-05-31 17:06:01 浏览: 112
统计字符串中数字、字母和空格的个数
5星 · 资源好评率100%
你可以使用Python编程语言来实现这个功能。以下是一个示例代码:
```python
string = input("请输入一个字符串:")
count_alpha = 0
count_digit = 0
count_space = 0
count_other = 0
for char in string:
if char.isalpha():
count_alpha += 1
elif char.isdigit():
count_digit += 1
elif char.isspace():
count_space += 1
else:
count_other += 1
print("字母个数:", count_alpha)
print("数字个数:", count_digit)
print("空格个数:", count_space)
print("其他字符个数:", count_other)
```
运行程序后,在控制台输入一个字符串,程序将会统计其中字母、数字、空格和其他字符的个数,并输出结果。
阅读全文