输入一个字符串,统计英语字母、数字字符、其它字符的个数。
时间: 2024-05-14 07:16:20 浏览: 13
可以使用 Python 的字符串方法和判断语句来统计英语字母、数字字符和其它字符的个数。以下是一个示例代码:
```python
s = input("请输入一个字符串:")
letters = 0 # 英语字母数量
digits = 0 # 数字字符数量
others = 0 # 其它字符数量
for c in s:
if c.isalpha(): # 判断是否为英语字母
letters += 1
elif c.isdigit(): # 判断是否为数字字符
digits += 1
else:
others += 1 # 其它字符数量加 1
print("英语字母数量:", letters)
print("数字字符数量:", digits)
print("其它字符数量:", others)
```
运行示例:
```
请输入一个字符串:Hello, World! 123
英语字母数量: 10
数字字符数量: 3
其它字符数量: 5
```
相关问题
Python输入一个字符串,分别统计其中字母数字和其他字符的个数
好的,这是一个编程问题。可以用 Python 的 isalpha()、isdigit() 函数和循环来统计字符串中的字母、数字和其他字符的个数。以下是示例代码:
```
s = input("请输入一个字符串:")
alpha_count = digit_count = other_count = 0
for ch in s:
if ch.isalpha():
alpha_count += 1
elif ch.isdigit():
digit_count += 1
else:
other_count += 1
print("字母个数:", alpha_count)
print("数字个数:", digit_count)
print("其他字符个数:", other_count)
```
输入一个字符串,程序会输出该字符串中字母、数字和其他字符的个数。
c语言实现:输入一个字符串,统计英语字母、数字字符、其它字符的个数。
以下是C语言实现,可以使用`ctype.h`头文件中的函数判断字符类型:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letter = 0, digit = 0, other = 0;
int i;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) { // 判断是否为英文字母
letter++;
} else if (isdigit(str[i])) { // 判断是否为数字字符
digit++;
} else { // 其它字符
other++;
}
}
printf("字母个数:%d\n", letter);
printf("数字个数:%d\n", digit);
printf("其它字符个数:%d\n", other);
return 0;
}
```
以上代码中,`fgets()`函数用于从标准输入中获取字符串,`isalpha()`函数判断字符是否为英文字母,`isdigit()`函数判断字符是否为数字字符。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)