← All examples

Character frequency

Java Loops

counting in an array

Flowchart (ISO 5807)

YesNoStartInput scount = new int[256]i = 0, s.length() - 1, 1count[s.charAt(i)]++c = 0, 255, 1count[c] > 0Output (char) c + «: » + count[c]EndFigure 1 — Freq.frequency

Source code

class Freq {
    static void frequency(String s) {
        int[] count = new int[256];
        for (int i = 0; i < s.length(); i++) {
            count[s.charAt(i)]++;
        }
        for (int c = 0; c < 256; c++) {
            if (count[c] > 0) {
                System.out.println((char) c + ": " + count[c]);
            }
        }
    }
}