c++输入一行字符串,计算出字符串的长度,并分别统计大写字母、小写字母、数字和其 
时间: 2023-05-02 14:06:40 浏览: 27
题目要求编写一个程序,输入一行字符串后,统计该字符串的长度,并分别统计出其中包含多少个大写字母、小写字母、数字和其他字符。以下是一个简单的Python程序实现:
```python
string = input("请输入一个字符串:") # 输入字符串
length = len(string) # 计算字符串长度
upper_count = 0 # 大写字母计数器
lower_count = 0 # 小写字母计数器
digit_count = 0 # 数字计数器
other_count = 0 # 其他字符计数器
for char in string:
if char.isupper():
upper_count += 1 # 大写字母累加
elif char.islower():
lower_count += 1 # 小写字母累加
elif char.isdigit():
digit_count += 1 # 数字累加
else:
other_count += 1 # 其他字符累加
print("输入的字符串长度为:", length)
print("其中包含大写字母", upper_count, "个")
print("其中包含小写字母", lower_count, "个")
print("其中包含数字", digit_count, "个")
print("其中包含其他字符", other_count, "个")
```
程序的实现比较简单,首先从用户处输入一个字符串,然后通过 `len()` 函数获取字符串的长度。接着定义四个计数器,用于统计大写字母、小写字母、数字和其他字符的数量。程序通过一个 for 循环遍历字符串的每一个字符,如果该字符是大写字母则累加到 `upper_count` 中,如果是小写字母则累加到 `lower_count` 中,如果是数字则累加到 `digit_count` 中,如果是其他字符则累加到 `other_count` 中。最后,程序输出长度、大写字母数、小写字母数、数字数和其他字符数。
相关推荐










