basics · beginner · ~10 min

Lowercase an ASCII string in place

ASCII-only case folding without external dependencies.

Challenge

Convert every uppercase ASCII letter in a string to lowercase, in place, leaving all other bytes untouched.

Task

Implement void to_lower_ascii(char *s) that lowercases each byte in the range A..Z (by adding 32) and leaves every other byte unchanged. The string is modified in place.

Input

A NUL-terminated, writable ASCII string s.

Output

No return value. s is rewritten in place with uppercase ASCII letters lowercased.

Example

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

Edge cases

  • Empty and already-lowercase strings are unchanged.
  • Digits and punctuation pass through.
  • Bytes with the high bit set (e.g. UTF-8 bytes like 0xC3) must NOT be modified.

Rules

  • ASCII only — do not touch non-ASCII (high-bit) bytes. No external case-folding helpers required.

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

A NUL-terminated writable ASCII string s.

Output format

No return value; s is lowercased in place.

Constraints

O(strlen). ASCII only — leave non-ASCII (high-bit) bytes untouched.

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.