1
Index-to-character conversion is the inverse of character-to-index mapping. Given an integer index i (0–25 for lowercase English), adding the base character’s ASCII value produces the corresponding character: ch = i + 'a'.
After building a frequency array, the stored data is indexed numerically (0–25). To produce human-readable output (printing “a = 3” not “0 = 3”), you must convert each index back to its character. This is the reverse of the ch - 'a' mapping and uses i + 'a'.
- During traversal of the frequency array, you have an index
i(0 to 25) - Compute
char ch = i + 'a' - Use
chfor output or further processing - For i=0 → ‘a’, i=1 → ‘b’, …, i=25 → ‘z’
- O(1) computation — simple integer addition
- Must match the base used in the forward mapping (
'a'forch - 'a') - Produces only lowercase letters when used with 0–25 indices
- No bounds checking — caller must ensure index is in valid range
- Built from: Character-to-Index Mapping — index-to-character is the mathematical inverse of character-to-index
- Builds into: Most Frequent Character — after finding the max index, convert back to character
- Builds into: Frequency Array — used during the traversal phase to produce output
- Contrasts with: Hash Map Traversal Method — maps store key-value pairs directly, no conversion needed
- Using
'A'as base when the mapping used'a'produces wrong letters - Indices outside 0–25 produce non-alphabetic characters (e.g., i=26 → ’{’)
- Forgetting this step and printing raw indices is a common beginner mistake
- When using uppercase mapping (
ch - 'A'), the reverse must usei + 'A'