basics · beginner · ~10 min

Lowercase an ASCII string in place

ASCII-only case folding without external dependencies.

Challenge

Implement void to_lower_ascii(char *s) that converts every uppercase ASCII letter ('A'..'Z') in s to its lowercase equivalent, in place. All other bytes are left unchanged.

Examples

"HELLO"     -> "hello"
"Hello, X!" -> "hello, x!"
"123abcXYZ" -> "123abcxyz"
""          -> ""

Edge cases

  • Bytes with the high bit set (e.g. UTF-8 continuation bytes) must NOT be touched.
  • Digits and punctuation pass through unchanged.

Why this matters

Case-folding is the canonical 'normalise this string' step in URL handling, header parsing, command matching, and search. ASCII-only folding is the safe baseline; full Unicode case-folding is a much harder problem.

Input format

Null-terminated mutable ASCII string.

Output format

s is mutated in place.

Constraints

O(strlen). Pure ASCII — do not touch non-ASCII bytes.

Starter code

void to_lower_ascii(char *s) { /* TODO */ }

Common mistakes

Using tolower without the unsigned char cast — undefined behaviour for high-bit bytes. Touching every byte (corrupts UTF-8 continuation bytes that happen to fall in the range 0x41-0x5A in a multi-byte sequence — actually UTF-8 continuation bytes are 0x80+, so this is safe with the ASCII guard).

Edge cases to handle

Empty string; already-lowercase string; mixed with digits and punctuation; high-bit bytes (must passthrough).

Complexity

O(strlen(s)).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.