• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

Character-to-index mapping is the transformation of a character to an integer index by subtracting the base character’s ASCII value. For lowercase English letters, index = ch - 'a' maps ‘a’→0, ‘b’→1, …, ‘z’→25. This enables using characters as array indices.

index(c)=ASCII(c)ASCII(base)\text{index}(c) = \text{ASCII}(c) - \text{ASCII}(\text{base})

Explanation

Computers store characters as integer codes (ASCII, UTF-8). When characters are contiguous in the encoding — as lowercase letters ‘a’–‘z’ are — subtracting the base character’s code produces a zero-based index. This is the bridge between the character domain and the array-index domain, and it is what makes frequency arrays possible.

How It Works

  1. Determine the base character (e.g., ‘a’ for lowercase letters, ‘A’ for uppercase)
  2. Get the ASCII value of the target character: int(ch)
  3. Subtract the ASCII value of the base character: int(ch) - int('a')
  4. The result is a zero-based index suitable for array access

For 'a' through 'z', ASCII values are 97–122, so ch - 'a' yields 0–25.

Mathematical Formulation

Given ASCII encoding where ASCII(a)=97\text{ASCII}('a') = 97:

idx(c)=code(c)code(a)\text{idx}(c) = \text{code}(c) - \text{code}('a')

idx(c){0,1,2,,25}for c{a,b,,z}\text{idx}(c) \in \{0, 1, 2, \dots, 25\} \quad \text{for } c \in \{'a', 'b', \dots, 'z'\}

Visual Explanation

char_to_index CHAR Character: 'n' ASCII ASCII Value: 110 CHAR->ASCII SUB 110 - 97 = 13 ASCII->SUB BASE Base 'a': 97 BASE->SUB INDEX Index: 13 SUB->INDEX

Semantic Network

semantic_char_to_index THIS Char-to-Index Mapping PRE1 ASCII Encoding THIS--PRE1 built from OUT1 Frequency Array THIS--OUT1 builds into OUT2 Index-to-Char Conversion THIS--OUT2 builds into CON1 ASCII Math Elimination THIS--CON1 contrasts with REL1 Known Range Assumption THIS--REL1 related REL2 Direct Array Access THIS--REL2 related

Key Properties

  • O(1) computation — simple integer subtraction
  • Requires characters to be contiguous in the encoding
  • Only works for a single case at a time (lowercase or uppercase, not both)
  • Assumes ASCII encoding (works in C++ on all major platforms)
  • The inverse operation is i + 'a'

Connections

Edge Cases & Gotchas

  • Applying ch - 'a' to an uppercase character ‘A’–‘Z’ (ASCII 65–90) gives negative indices — undefined behavior
  • Applying it to digits, punctuation, or spaces gives unpredictable indices
  • Mixing cases silently produces wrong results — ‘A’ maps to -32 (wraps around for unsigned, negative for signed)
  • C++ char may be signed or unsigned depending on platform — ch - 'a' with negative char values is implementation-defined
  • The mapping assumes ASCII; EBCDIC systems do not have contiguous letters