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 put together a char bubble sort for you. There are other ways to do this with less code, using built in sorting methods, but this one shows how an array sort works.

This does not take into account Upper and lower case letters which would change their numeric Unicode value, but this will get you going in the right direction.

```
    public static void main(String[] args) {

    char[] str = "cabababb".toCharArray(); //Turn your string into an array

    int n = str.length; //get the length of the array
    char temp; //a temporary character holder
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {
            int compare = Character.getNumericValue(str[j - 1]);//get the numeric value of the char
            int compareTo = Character.getNumericValue(str[j]); //etc
            if (compare > compareTo) {//do a compare
                //swap elements  
                temp = str[j - 1]; //bubble sort
                str[j - 1] = str[j];
                str[j] = temp;
            }

        }
    }
    System.out.println(Arrays.toString(str));//You get your sorted array

}

```

The concept behind a bubble sort does this:

if your array read ['x','j']

these two variables will step into the if statement with comment `//do a compare` if the previous array iteration is greater than the current array iteration:

1. it will put the previous char into a "temp" variable
2. then it will replace the previous char with the current char
3. finally, it will put the current char as the previous char(saved as "temp")

So going back to the example: 

> 
> ['x','j']
> 
> 
> 

```
if('x' > 'j'){//for example's sake **Not real code**
//x is in 0 position
//j is in 1 position
                temp = 0 position's value; 
                0 position's value = 1 position's value;
                1 position's value = temp;
}

```

At the end you are left with an alphabetized array. 

If you need it back into a string you can concatenate it back into a string

```
String newStr = "";

        for(char letter : str){

           newStr += letter;

        }

        System.out.println(newStr);

```

I hope this helps you understand the concept behind sorting in Java