从键盘输入一行字符,统计其中英文字母的个数。
时间: 2023-06-05 21:47:12 浏览: 96
可以使用循环遍历输入的字符串,判断每个字符是否为英文字母,如果是则计数器加1。最后输出计数器的值即可。具体代码如下:
```python
s = input("请输入一行字符:")
count =
for c in s:
if c.isalpha():
count += 1
print("英文字母的个数为:", count)
```
其中,`isalpha()`方法用于判断一个字符是否为英文字母。
相关问题
从键盘输入一行字符,统计其中英文字母的个数
```
#include <stdio.h>
int main() {
char str[100];
int count = 0;
printf("请输入一行字符:\n");
fgets(str, 100, stdin); // 从键盘输入一行字符
for (int i = 0; str[i] != '\0'; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
count++;
}
}
printf("英文字母的个数为:%d\n", count);
return 0;
}
```
输入样例:
```
请输入一行字符:
Hello, World!
```
输出样例:
```
英文字母的个数为:11
```
python 从键盘输入一行字符,分别统计其中英文字母,空格,数字和其它字符的个数
下面是Python代码实现:
```python
s = input("请输入一行字符:")
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print("字母个数:", letters)
print("空格个数:", space)
print("数字个数:", digit)
print("其他字符个数:", others)
```