统计中英文字符,数字,空格个数

public class LianXi07 {
	public static void main(String[] args) {
		int digital = 0;
		int character = 0;
		int blank = 0;
		int other = 0;
		char[] ch = null;
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		ch = s.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] >= '0' && ch[i] <= '9') {
				digital++;
			} else if (ch[i] > 'a' && ch[i] <= 'z') {
				character++;
			} else if ((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] > 'A' && ch[i] <= 'Z') {
				character++;

			} else if (ch[i] == ' ') {
				blank++;
			} else {
				other++;
			}
		}
		sc.close();
		System.out.println("数字个数: " + digital);
		System.out.println("英文字母个数: " + character);
		System.out.println("空格个数: " + blank);
		System.out.println("其他字符个数:" + other);

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用Python编写程序来统计一个字符中英文字符空格数字和其他字符个数。具体步骤如下: 1. 首先定义一个字符串,可以是一个变量或者直接在程序中写入字符串。 2. 然后使用Python内置函数isalpha()、isdigit()和isspace()来判断每个字符是英文字符数字还是空格。 3. 使用for循环遍历字符串中的每个字符,将其分类并记录个数。 4. 最后输出每个分类的个数。 以下是一个示例程序: ```python string = "Hello, World! 123" count_en = 0 count_space = 0 count_digit = 0 count_other = 0 for char in string: if char.isalpha(): count_en += 1 elif char.isspace(): count_space += 1 elif char.isdigit(): count_digit += 1 else: count_other += 1 print("英文字符个数:", count_en) print("空格个数:", count_space) print("数字个数:", count_digit) print("其他字符个数:", count_other) ``` 输出结果为: ``` 英文字符个数: 10 空格个数: 2 数字个数: 3 其他字符个数: 2 ``` 以上就是用Python统计字符中英文字符空格数字和其他字符的方法。 ### 回答2: 可以使用Python编写函数来统计给定字符中英文字符空格数字和其他字符个数。以下是一种可能的实现方法: ```python def count_characters(string): count_alpha = 0 count_digit = 0 count_space = 0 count_other = 0 for char in string: if char.isalpha(): # 判断字符是否是英文字符 count_alpha += 1 elif char.isdigit(): # 判断字符是否是数字 count_digit += 1 elif char.isspace(): # 判断字符是否是空格 count_space += 1 else: # 其他字符 count_other += 1 return count_alpha, count_digit, count_space, count_other # 测试函数 string = "Hello World! 123" result = count_characters(string) print("英文字符个数:", result[0]) print("数字个数:", result[1]) print("空格个数:", result[2]) print("其他字符个数:", result[3]) ``` 输出结果: ``` 英文字符个数: 10 数字个数: 3 空格个数: 2 其他字符个数: 1 ``` 这段代码中定义了一个`count_characters`函数,接受一个字符串作为参数。然后,使用循环遍历字符串的每个字符,并使用`isalpha()`、`isdigit()`和`isspace()`等内置方法来判断字符的类型,从而进行统计。最后,返回各类型字符个数,并进行打印输出。 ### 回答3: 要实现这个功能,我们可以使用Python的字符串处理函数和一些控制流。 首先,我们需要记录英文字符数、空格数、数字数和其他字符数的计数器。 然后,通过遍历字符串中的每一个字符,通过判断字符的类型来将计数器相应地递增。 以下是一个简单的实现: ```python def count_characters(string): english_count = 0 space_count = 0 digit_count = 0 other_count = 0 for char in string: if char.isalpha(): english_count += 1 elif char.isspace(): space_count += 1 elif char.isdigit(): digit_count += 1 else: other_count += 1 return english_count, space_count, digit_count, other_count input_string = input("请输入一个字符串:") english, space, digit, other = count_characters(input_string) print("英文字母个数:", english) print("空格个数:", space) print("数字个数:", digit) print("其他字符个数:", other) ``` 这段代码首先定义了一个`count_characters`函数,它接受一个字符串作为输入。函数内部,我们使用四个计数器变量来保存各个类型字符的数量。然后,通过循环遍历字符串中的每一个字符,根据字符的类型将相应的计数器递增。 最后,我们在主程序中获取用户输入的字符串,并调用`count_characters`函数来获取各个类型字符的数量,然后打印出统计结果。 希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小阳世界2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值