Question: I need to find an algorithm which counts the letters in any string (e.g. "cabababb") and the output must be in a alphabetical manner(e.g. a: 3, b: 4, c:1 etc.) . There is no need to distinguish between upper and lower case letters. I am not allowed to youse a HashMap.

This is my code till now: I'ts not working, I think I've got the wrong strategy here. Can you help me please?

```
public class Stringray extends MiniJava{
public static void main(String[] args) {
    // Texteingabe
    String input = readString("Geben Sie einen Text ein: ");

    String output = "";
    int i = 0;

    // Häufigkeit der Buchstaben
    int count = 0;
    char letter = 'a';
    while(i < input.length()) {
            while(i < input.length()) {
                if(input.charAt(i) == letter) {
                    while(i < input.length()) {
                        count++;
                        i++;
                    }
                    output = output + letter + ": " + count + "\t";
                }
                i++;
            }
            letter++;
    }
    write(output);
}
}

```

Answer: 
Here's a simple algorithm:

1. Create an empty `int[]` array of length `26`, it will be initialized with `0`s
2. Iterate your String
3. Get the `char` at the current index.
4. Tricky part: You need a mapping between a given `char` and its corresponding index (e.g. `a` would be index `0`, `b` would be index `1` and so on). Have a look at an ASCII-Table for this
5. Add 1 to the element at the mapped index of the created array.
6. At the end, iterate the array and only print the values that are not `0` along with the corresponding `char` at that index (Need the ASCII mapping again here)
A really preformant way to do this in linear time is using a HashMap

```
public class Stringray extends MiniJava {

    public static void main(String[] args) {
        String input = "Geben Sie einen Text ein: ";
        System.out.println(countLetters(input));
    }

    public static HashMap<Character, Integer> countLetters(String input) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        final int length = input.length();
        for (int i = 0; i < length; i++) {
            Character currentChar = Character.toLowerCase(input.charAt(i));
            if (Character.isAlphabetic(currentChar)) {
                if (hashMap.containsKey(currentChar)) {
                    hashMap.put(currentChar, hashMap.get(currentChar) + 1);
                } else {
                    hashMap.put(currentChar, 1);
                }
            }
        }
        return hashMap;
    }

}

```

You need to consider that Upper and Lower case are the same and discard non alphabetic characters. Once you have all the information in the hashmap, the way you show it console is another matter. If you need to show a zero for the letters that are not present in your text it would be better to initialize the hashmap to that.