Count letters in a string

Here’s a simple algorithm:

  1. Create an empty int[] array of length 26, it will be initialized with 0s
  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)

Leave a Comment