package com.example;
import java.util.Scanner;
/**
* @author zhanghong
* @date 2020/5/21 11:01
* @Description: 统计字符串中各种字符出现的次数
*/
public class DemoStringCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String input = scanner.next();
int countUpper = 0;
int countLower = 0;
int countNumber = 0;
int countOther = 0;
char[] chars = input.toCharArray();
for (char ch : chars) {
if ('A' <= ch && ch <= 'Z') {
countUpper++;
} else if ('a' <= ch && ch <= 'z') {
countLower++;
} else if ('0' <= ch && ch <= '9') {
countNumber++;
} else {
countOther++;
}
}
System.out.println("大写:"+countUpper);
System.out.println("小写:"+countLower);
System.out.println("数字:"+countNumber);
System.out.println("其他字符:"+countOther);
}
}
统计字符串中各种字符出现的次数
最新推荐文章于 2023-07-11 15:51:29 发布
本文介绍如何使用编程语言统计一个字符串中各个字符出现的次数,包括字母、数字和其他特殊字符。通过对字符串遍历和计数,可以得到每个字符的频率,这对于数据分析和文本处理任务非常有用。
536

被折叠的 条评论
为什么被折叠?



