输入一个以回车符结束的字符串,分别统计其中的大写字母、小写字母、数字和其他字符的个数。
时间: 2023-12-14 11:02:07 浏览: 53
可以使用 Python 来实现此功能,代码如下:
```python
string = input("请输入一个字符串:")
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("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("其他字符个数:", other_count)
```
运行代码后,会提示你输入一个字符串,输入完毕后,程序会按照要求进行统计并输出结果。
相关问题
用c语言编写:输入一个以回车符结束的字符串,分别统计其中的大写字母、小写字母、数字和其他字符的个数。
```c
#include <stdio.h>
int main() {
char str[100];
int i, uppercase = 0, lowercase = 0, digit = 0, other = 0;
printf("请输入字符串:");
fgets(str, sizeof(str), stdin); // 使用 fgets() 输入字符串,以避免输入超出数组长度
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
uppercase++;
} else if (str[i] >= 'a' && str[i] <= 'z') {
lowercase++;
} else if (str[i] >= '0' && str[i] <= '9') {
digit++;
} else {
other++;
}
}
printf("大写字母个数:%d\n", uppercase);
printf("小写字母个数:%d\n", lowercase);
printf("数字个数:%d\n", digit);
printf("其他字符个数:%d\n", other);
return 0;
}
```
输入一行字符回车结束编程统计其中大写字母小写字母数字字符空格和其他字符的个数
```python
string = input("请输入一行字符:")
upper_count = 0
lower_count = 0
digit_count = 0
space_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
elif char.isspace():
space_count += 1
else:
other_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("空格个数:", space_count)
print("其他字符个数:", other_count)
```
示例输出:
```
请输入一行字符:Hello, World! 123
大写字母个数: 2
小写字母个数: 8
数字个数: 3
空格个数: 2
其他字符个数: 2
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)