Lesson 24 of 34

Characters and ASCII

9 min 30 XP

Every char has a number (its ASCII/Unicode code): 'A' = 65, 'a' = 97, '0' = 48. So you can do maths with chars: 'a' + 1 is 'b'.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Count letter frequencies with an int array of size 26: count[ch - 'a']++. This trick solves anagram and first-non-repeating-character questions.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Challenge

+30 XP

Read two lowercase words. Print "Anagram" if they use exactly the same letters the same number of times, otherwise "Not anagram". Input: listen silent → Output: Anagram

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.