1

I have searched and found some similar questions, but just can't seem to figure out how to apply them to my problem. Please bear with me, as this is my first post on the site.

I have to find out how many times a character of a string repeats itself. For example, if given "launch at noon", the method should print:

"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".
Khris
  • 3,132
  • 3
  • 34
  • 54
Scott Ping
  • 11
  • 1
  • Can you share some code you have already done? Or an idea how to solve this? – Flummox - don't be evil SE Oct 19 '16 at 13:41
  • `String.charAt`, `String.length`, `for loop`. That's all you need. – Murat Karagöz Oct 19 '16 at 13:46
  • till where have you got the coding to? any progress? – Shreyas Sarvothama Oct 19 '16 at 13:47
  • 1
    Similar question asked here: (http://stackoverflow.com/questions/15903077/how-to-find-how-many-times-does-a-string-object-repeat-in-java) – Todd Oct 19 '16 at 13:50
  • @Todd It is not even similar --- when you look at that question, you just turn the "object" search term into "o", and you got exactly this problem. Thus: good catch! – GhostCat Oct 19 '16 at 13:52
  • Oh guys you are so mean marking as duplicate with a question with 2 thumbs before 3 years. You should have let Scott with his first question to get some answers, thumbs and whatever. – Lazar Lazarov Oct 19 '16 at 13:59
  • Honestly, I'm struggling as to where to even start. We are currently learning about for loops, so I know I need to use that. I have a similar piece of code I believe holds the answer, but I can't understand how I need to adjust it to what I need. //count how many times c appears on s static int countChar(String s, char c) { int cnt = 0; for (int i = 0; i < s.length(); i++) if (s.charAt(i) == c) cnt++; return cnt; } Crap, I don't know how to post code either :/ – Scott Ping Oct 19 '16 at 15:39

1 Answers1

0
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",
};
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213