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: 
You can solve the problem by following the steps below like QBrute correctly pointed out.

```
public String countMatches(String main) {
    //Create an array of the alphabets length
    main = main.toLowerCase();
    int[] foundArray = new int[26];
    String output = "";
    //Create an alphabets array
    String[] alphabets = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split("\\s");
    //Split the main String array
    String[] mainArray = main.split("");
    for (String string : mainArray) {
        //Iterate through the array
        for (int i = 0; i < alphabets.length; i++) {
            //If the string index matches any of our alphabets
            if (string.equals(alphabets[i])) {
                //Increase the corresponding foundArray index value
                foundArray[i] = foundArray[i] + 1;
            }
        }
    }
    //Iterate through the foundArray
    for (int i = 0; i < foundArray.length; i++) {
        //If an index is greater than 0
        if (foundArray[i] > 0) {
            //A match was found
            //Assign the matched foundArray index to the corresponding alphabet
            output += alphabets[i] + ":" + foundArray[i] + ", ";
        }
    }
    return output;
}

```
I am unsure whether this is a trick question. But really all you have to do it seems is to create a method that counts the number of occurrences of a character in String. The code below how to do this is the easiest way possible. I leave it to you to modify it such that it fits all your testcases.

```
public static void main(String[] args)
{
    String input = "cabababb";
    char[] testCharacters = new char[] { 'a', 'b', 'c' };
    for (int i = 0; i < testCharacters.length; i++)
    {
        System.out.println(testCharacters[i] + " occurs " + countOccurencesOf(testCharacters[i], input));
    }
}

public static int countOccurencesOf(char aCharacter, String inThisString)
{
    int count = 0;
    for (int i = 0; i < inThisString.length(); i++)
    {
        if (aCharacter == inThisString.charAt(i)) { count++; }
    }
    return count;
}

```