编程实现:从键盘输入任意一个字符串,统计大写英文字母和小写英文字母的个数。
时间: 2024-05-15 07:15:07 浏览: 103
```python
s = input("请输入一个字符串:")
upper_count = 0 # 大写字母计数器
lower_count = 0 # 小写字母计数器
for char in s:
if char.isupper(): # 如果是大写字母
upper_count += 1
elif char.islower(): # 如果是小写字母
lower_count += 1
print("大写字母个数为:", upper_count)
print("小写字母个数为:", lower_count)
```
示例输出:
```
请输入一个字符串:Hello, World!
大写字母个数为: 2
小写字母个数为: 8
```
相关问题
编程实现:从键盘输入任意一个字符串,统计大写英文字母和小写英文字母的个数
```python
string = input("请输入一个字符串:")
count_upper = 0 # 大写字母个数
count_lower = 0 # 小写字母个数
for char in string:
if char.isupper(): # 判断是否为大写字母
count_upper += 1
elif char.islower(): # 判断是否为小写字母
count_lower += 1
print("大写字母个数:", count_upper)
print("小写字母个数:", count_lower)
```
样例输入:
```
Hello World!
```
样例输出:
```
大写字母个数: 2
小写字母个数: 8
```
以c语言的编程实现:从键盘输入一行字符,统计其大写字母、小写字母、空格、数字和其它字符的个数。
在C语言中,你可以通过以下步骤实现这个功能:
```c
#include <stdio.h>
#include <ctype.h> // 包含处理字符类型的库
int main() {
int uppercase = 0, lowercase = 0, spaces = 0, digits = 0, others = 0;
char input[100];
printf("请输入一行字符:");
fgets(input, sizeof(input), stdin); // 从键盘读取一行输入
for (int i = 0; i < strlen(input); i++) { // 遍历输入字符串
if (isupper(input[i])) { // 判断字符是否为大写字母
uppercase++;
} else if (islower(input[i])) { // 判断字符是否为小写字母
lowercase++;
} else if (isspace(input[i])) { // 判断字符是否为空格
spaces++;
} else if (isdigit(input[i])) { // 判断字符是否为数字
digits++;
} else { // 其他字符(非字母、数字或空格)
others++;
}
}
printf("大写字母:%d\n", uppercase);
printf("小写字母:%d\n", lowercase);
printf("空格:%d\n", spaces);
printf("数字:%d\n", digits);
printf("其他字符:%d\n", others);
return 0;
}
```
当你运行这个程序,它会提示用户输入一行字符,并随后显示各类型字符的数量。
阅读全文
相关推荐
![](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)