从键盘输入一行字符,统计其中大写字符的个数,小写字符的个数,数字的个数,以及非字母和数字的个数。请分别用for,while循环语句实现 从键盘输入一行字符,统计其中大写字符的个数,小写字符的个数,数字的个数,以及非字母和数字的个数。请分别用for,while循环语句实现
时间: 2024-05-14 16:14:05 浏览: 26
tj.rar_visual c_数字字符个数
使用for循环实现:
```python
s = input("请输入一行字符:")
upper_count = 0
lower_count = 0
digit_count = 0
other_count = 0
for c in s:
if c.isupper():
upper_count += 1
elif c.islower():
lower_count += 1
elif c.isdigit():
digit_count += 1
else:
other_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("非字母和数字个数:", other_count)
```
使用while循环实现:
```python
s = input("请输入一行字符:")
upper_count = 0
lower_count = 0
digit_count = 0
other_count = 0
i = 0
while i < len(s):
if s[i].isupper():
upper_count += 1
elif s[i].islower():
lower_count += 1
elif s[i].isdigit():
digit_count += 1
else:
other_count += 1
i += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("非字母和数字个数:", other_count)
```
阅读全文