【问题标题】:How do I find out how many times each character of a string 's' repeats itself [duplicate]如何找出字符串's'的每个字符重复自身的次数[重复]
【发布时间】:2016-10-19 13:40:24
【问题描述】:

我已经搜索并发现了一些类似的问题,但似乎无法弄清楚如何将它们应用于我的问题。请多多包涵,因为这是我在网站上的第一篇文章。

我必须找出一个字符串中的一个字符重复了多少次。例如,如果给定"launch at noon",该方法应该打印:

"l" appears once in "launch at noon".
"a" appears twice in "launch at noon".
"u" appears once in " launch at noon ".
"n" appears 3 times in "launch at noon".
"c" appears once in "launch at noon".
"h" appears once in "launch at noon".
" " appears twice in "launch at noon".
"t" appears once in "launch at noon".
"o" appears twice in "launch at noon".

【问题讨论】:

  • 你能分享一些你已经完成的代码吗?或者一个想法如何解决这个问题?
  • String.charAtString.lengthfor loop。这就是你所需要的。
  • 你的编码到哪里去了?有什么进展吗?
  • 在这里提出了类似的问题:(stackoverflow.com/questions/15903077/…)
  • @Todd 它甚至不相似 --- 当你看那个问题时,你只要把“object”搜索词变成“o”,你就得到了这个问题。因此:很好的收获!

标签: java


【解决方案1】:
public void test() {
    // The string to test.
    String s = "launch at noon";
    // Count them.
    Map<Character, Integer> characterCounts = countCharacters(s);
    // Print the results.
    for (Map.Entry<Character, Integer> e : characterCounts.entrySet()) {
        int count = e.getValue();
        // Work out the name for the number.
        String number = count < specials.length 
                // The special short ones.
                ? specials[count] 
                // Is it a general one?
                : count < numbersAsWords.length
                // Yes
                ? numbersAsWords[count] + " times"
                // Way too many.
                : "too many times";
        // Print it.
        System.out.println("\"" + e.getKey() + "\" appears " + number + " in \"" + s + "\".");
    }
}

private Map<Character, Integer> countCharacters(String s) {
    // Map from character in string to count of occurrences.
    Map<Character, Integer> counts = new HashMap<>();
    // Walk the string.
    for (int i = 0; i < s.length(); i++) {
        // Get character at that point.
        char ch = s.charAt(i);
        // What's in the map for that character.
        Integer count = counts.get(ch);
        if (count == null) {
            // Not seen this character before - start at zero.
            count = 0;
        }
        // Increment seen count.
        count += 1;
        // Put it back in the map.
        counts.put(ch, count);
    }
    return counts;
}

// The short ones.
private static final String[] specials = {"not at all", "once", "twice"};

// The rest.
private static final String[] numbersAsWords = {"zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
        "twenty",
        "twenty-one",
        "twenty-two",
        "twenty-three",
        "twenty-four",
        "twenty-five",
        "twenty-six",
        "twenty-seven",
        "twenty-eight",
        "twenty-nine",
        "thirty",
        "thirty-one",
        "thirty-two",
        "thirty-three",
        "thirty-four",
        "thirty-five",
        "thirty-six",
        "thirty-seven",
        "thirty-eight",
        "thirty-nine",
        "forty",
        "forty-one",
        "forty-two",
        "forty-three",
        "forty-four",
        "forty-five",
        "forty-six",
        "forty-seven",
        "forty-eight",
        "forty-nine",
        "fifty",
        "fifty-one",
        "fifty-two",
        "fifty-three",
        "fifty-four",
        "fifty-five",
        "fifty-six",
        "fifty-seven",
        "fifty-eight",
        "fifty-nine",
        "sixty",
        "sixty-one",
        "sixty-two",
        "sixty-three",
        "sixty-four",
        "sixty-five",
        "sixty-six",
        "sixty-seven",
        "sixty-eight",
        "sixty-nine",
        "seventy",
        "seventy-one",
        "seventy-two",
        "seventy-three",
        "seventy-four",
        "seventy-five",
        "seventy-six",
        "seventy-seven",
        "seventy-eight",
        "seventy-nine",
        "eighty",
        "eighty-one",
        "eighty-two",
        "eighty-three",
        "eighty-four",
        "eighty-five",
        "eighty-six",
        "eighty-seven",
        "eighty-eight",
        "eighty-nine",
        "ninety",
        "ninety-one",
        "ninety-two",
        "ninety-three",
        "ninety-four",
        "ninety-five",
        "ninety-six",
        "ninety-seven",
        "ninety-eight",
        "ninety-nine",
        "one hundred",
};

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 2021-01-11
    • 2018-01-30
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多