输入一行字符,分别统计出其中英文字母、空格、数宇和其他字符的个数
时间: 2023-11-16 19:04:10 浏览: 34
请问您需要什么语言的代码呢?以下是Python和Java的示例代码:
Python代码:
```python
s = input("请输入一行字符:")
letters = 0
space = 0
digits = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digits += 1
else:
others += 1
print("英文字母个数:", letters)
print("空格个数:", space)
print("数字个数:", digits)
print("其他字符个数:", others)
```
Java代码:
```java
import java.util.Scanner;
public class CharacterCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一行字符:");
String s = sc.nextLine();
int letters = 0;
int space = 0;
int digits = 0;
int others = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetter(c)) {
letters++;
} else if (Character.isWhitespace(c)) {
space++;
} else if (Character.isDigit(c)) {
digits++;
} else {
others++;
}
}
System.out.println("英文字母个数:" + letters);
System.out.println("空格个数:" + space);
System.out.println("数字个数:" + digits);
System.out.println("其他字符个数:" + others);
}
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)