统计字符串中的每个字符出现次数
2017-04-08 13:58
295 查看
手动输入一个字符串,仅限小写字母,统计并输出每个字符在字符串中出现的次数,并输出。提示(可以用Map)
代码结果:
a=10
b=5
c=4
d=2
e=4
f=4
g=4
i=1
j=1
k=2
l=2
n=2
o=1
r=1
s=4
W=2
x=1
z=1
;=4
package 华为机试题; import java.util.HashMap; import java.util.Map; /** * @author Hutongling * * @time:2017年4月8日 下午1:43:49 */ public class 统计字符串中的每个字符出现次数 { static Map<Character, Integer> countString(String string) { if (string == null || string.length() == 0) return null; Map<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < string.length(); i++) map.put(string.charAt(i), 0); for (int i = 0; i < string.length(); i++) { if (map.containsKey(string.charAt(i))) map.put(string.charAt(i), map.get(string.charAt(i)) + 1); } return map; } public static void main(String[] args) { String string = "aaabbbccceefegifa;;lskgnao;dgbasdzxc;aeraffasgnsjlakbWW"; Map<Character, Integer> map = countString(string); for (Character a : map.keySet()) System.out.println(a + "=" + map.get(a)); } }
代码结果:
a=10
b=5
c=4
d=2
e=4
f=4
g=4
i=1
j=1
k=2
l=2
n=2
o=1
r=1
s=4
W=2
x=1
z=1
;=4
相关文章推荐
- 输入任意字符串,统计字符串中每个字符出现的次数
- JavaScript统计字符串中每个字符出现次数完整实例
- 统计字符串中每个字符出现的次数
- Java小程序:字符串中,统计每个字符出现的次数
- 统计字符串中每个字符出现的次数
- 第十三周 项目五--字符串操作(1.3)统计每个字符出现次数
- 统计某字符串中每个字符出现的次数
- C++统计字符串中每个字符出现的次数
- hashmap统计字符串中每个字符出现的次数
- JavaScript统计字符串中每个字符出现次数完整实例
- 从控制台获取一串字符串,并对每个字符出现的次数进行统计
- 统计字符串中每个字符出现的次数 打印结果是:a(2)b(1)
- 小方法1——CountChar--统计一个字符串中每个字符出现的次数
- 华为机试题---统计并输出每个字符在字符串中出现的次数
- Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例
- 统计字符串”abadcdffbaeba”中每个字符出现了多少次,按次数排序并输出
- 统计字符串中每个字符出现的次数
- 统计一个字符串中每个字符出现的次数
- 利用自然顺序统计字符串中每个字符的出现次数
- 统计一个字符串中每个字符出现的次数,并按自然顺序输出