统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
时间: 2023-04-26 08:04:08 浏览: 63
你好!要统计一行字符串中的英文字母个数、空格个数、数字个数和其他字符个数,可以使用循环遍历字符串,然后对每个字符进行分类计数。
以下是示例代码:
```python
s = "Hello World 123!?"
letters = 0
spaces = 0
digits = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
spaces += 1
elif c.isdigit():
digits += 1
else:
others += 1
print("英文字母个数:", letters)
print("空格个数:", spaces)
print("数字个数:", digits)
print("其他字符个数:", others)
```
输出结果如下:
```
英文字母个数: 10
空格个数: 1
数字个数: 3
其他字符个数: 2
```
希望这个例子能帮到你!
相关问题
C语言统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
下面是一个示例代码,可以统计一个字符串中的英文字母个数、空格个数、数字个数、其他字符个数:
```c
#include <stdio.h>
int main() {
char str[100];
int i, alphabets, digits, spaces, others;
printf("请输入一个字符串:");
gets(str);
alphabets = digits = spaces = others = 0;
for (i = 0; str[i] != '\0'; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
alphabets++;
} else if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else if (str[i] == ' ') {
spaces++;
} else {
others++;
}
}
printf("英文字母个数:%d\n", alphabets);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
在这个示例中,我们使用了一个 `for` 循环来遍历输入的字符串。在循环中,我们使用了一些简单的条件语句来判断当前字符是英文字母、数字、空格还是其他字符。然后,我们分别增加了相应的计数器。最后,我们打印出每个计数器的值,以显示字符串中各种字符的个数。
用java统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
可以使用Java中的字符类型判断每个字符的类型,从而计算出英文字母个数、空格个数、数字个数、其他字符个数。
以下是一个示例代码:
```java
public static void countChars(String s) {
int letters = 0;
int spaces = 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.isDigit(c)) {
digits++;
} else if (Character.isSpaceChar(c)) {
spaces++;
} else {
others++;
}
}
System.out.println("Letters: " + letters);
System.out.println("Spaces: " + spaces);
System.out.println("Digits: " + digits);
System.out.println("Others: " + others);
}
```
在这个示例代码中,我们定义了一个 `countChars` 方法,用于统计字符串中的英文字母个数、空格个数、数字个数、其他字符个数。我们使用了 `Character` 类中的静态方法来判断每个字符的类型。最后,我们通过打印输出的方式展示了统计结果。
你可以通过调用 `countChars` 方法并传入需要统计的字符串来进行测试,例如:
```java
countChars("Hello, world! 123");
```
输出结果如下:
```
Letters: 10
Spaces: 2
Digits: 3
Others: 2
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)