找出单词中每个字母的数量 [英] find the number of every letters in a word

查看:59
本文介绍了找出单词中每个字母的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户将输入一个字符串,例如,如果用户输入YYYCZZZZGG:程序将评估字符串中字符的频率.对于YYYCZZZZGG字符串,仅C可见1,G重复2,Y的频率为3,Z的频率为4.

User will enter a String, For instance if the user enters YYYCZZZZGG: Program will evaluate the frequency of characters in the string. For YYYCZZZZGG string, C is seen only for 1, G is repeated 2, Y has a frequency of 3, and Z’s frequency is 4.

找到每个字母的编号后,如何使用输出的程序编号绘制条形图?

after finding number of each letter how can I draw a bar graph using the numbers of the programs output?

推荐答案

尝试一下:

public static void main(String[] args) {
    String input = "YYYCZZZZGG";
    Map<Character, Integer> map = new HashMap<Character, Integer>(); // Map
    // to store character and its frequency.
    for (int i = 0; i < input.length(); i++) {
        Integer count = map.get(input.charAt(i)); // if not in map
        if (count == null)
            map.put(input.charAt(i), 1);
        else
            map.put(input.charAt(i), count + 1);
    }
    System.out.println(map);
}

输出:

{G=2, C=1, Y=3, Z=4}

这篇关于找出单词中每个字母的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆