Harish Shivaraj wrote:Remove your print statement from inside the loop to outside.
Paul Clements wrote:
Saw this idea a few weeks ago and it stuck with me. Seems a really clever way to count occurrences of A-Z in a string, with a minimum of fuss.
To explain. We are taking advantage of the fact that A-Z in char is 65-90. Therefore by subtracting 65 from the current char you end up with a number between 0-25. This provides you with an array index to store the count for that particular char. To display you just add 65 to the current array index and display the integer held in that slot.
i know the 1 times, 2 times its a result of the loop. as it is counting the number of times a is present. the loop goes trhoug finds an a, outputs the println statement,
the loop goes around again finds another a, outputs the println statement. If i could get the println statement outside of the for loop i would avoid this result, but if i do then the variables inside of the loop block are out of scope and cat call them.