统计字符串中的每个字母出现次数

统计字符串中的每个字母出现次数

实现:

public class Test {
 
	public static void main(String[] args) {
		String str = "axnajjnccka";
		//字符串转字符数组
		char []a = str.toCharArray();
		char []b =a;
		
		System.out.println(a);
		
		//遍历字符数组
		for (int i = 0; i < b.length; i++) {
			int key = 0;
			//逐个比较字符
			for (int j = 0; j < b.length; j++) {
				if(a[i] == b[j]) {
					key++;
				}
			}
			System.out.println(a[i]+"("+key+")");
		}
		
	}
 
}

结果:

猜你喜欢

转载自blog.csdn.net/qq_40270579/article/details/82556560
今日推荐