package mianshiti;
import java.util.Map;
import java.util.TreeMap;
public class T5 {
/**
* * 5.罗斯福名言“the only thing we have to fear itself”,统计每个字母出现的次数(按照字母表顺序),
* 输出格式为:a(3)e(6)f(3)g(1)h(3)i(3)......
*/
public static void main(String[] args) {
String str = "the only thing we have to fear itself";
char[] arr = str.toCharArray();
TreeMap<Character, Integer> tm = new TreeMap<>();
for (char c : arr) {
if(!(c == ' ')) {
tm.put(c, !tm.containsKey(c) ? 1 : tm.get(c) + 1);
}
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<Character, Integer> mp : tm.entrySet()) {
String str1 = String.valueOf(mp.getKey());
sb.append(str1 + "(" + mp.getValue() + ")");
}
System.out.println(sb);
}
}
|
|