String Reverser

Reverse a string character by character.

About This Tool

String reversal returns a sequence of characters in reverse order: 'hello' becomes 'olleh'. Trivial for ASCII, less trivial for Unicode — surrogate pairs (emoji, certain CJK characters) and combining marks can produce broken output if reversed naively at the code-unit level.

The reverser handles the common case (any input string returns its reverse) with awareness of basic UTF-16 surrogate pairs. Edge cases like grapheme clusters (a base letter plus combining marks treated as one visual character) require segmenter-aware reversal that pure character-by-character iteration breaks.

The naive implementation iterates from end to start, building a new string. For ASCII this is correct and runs in O(n) time. The complications start with Unicode. JavaScript and most languages with UTF-16 internal strings use 16-bit code units; characters outside the Basic Multilingual Plane (most emoji, mathematical alphanumerics, some historical scripts) are encoded as two-unit surrogate pairs. Reversing at the code-unit level splits these pairs, producing invalid characters. The fix: iterate by code point, which most modern languages support via spread operators ([...str]) or dedicated APIs (string.codePointAt). Even that's not enough for grapheme clusters — base letter plus combining marks (an 'e' with an acute accent expressed as e + ´) reverse to mark-then-base, breaking the visual character. Full grapheme awareness requires Intl.Segmenter or an equivalent library.

A worked example. Input: 'Hello, 世界! 👋'. Naive char-by-char reverse: '👋 !界世 ,olleH' if the implementation is code-point-aware. Naive code-unit reverse breaks the emoji into invalid surrogate halves. With grapheme awareness: same result as code-point. Input with combining marks: 'café' where é is e + combining acute (U+0065 U+0301). Naive reverse: '́efac' — the combining mark now sits on the wrong character. Grapheme-aware reverse: 'éfac' — visually correct. The difference is invisible in plain ASCII text but matters for any internationalized application.

Limitations and practical use. Time complexity is O(n) for n characters. Memory is also O(n) since strings are typically immutable in modern languages — you can't reverse in place without converting to a mutable buffer first. Practical applications outside puzzles include reversing digits to check for divisibility patterns, formatting numbers from least-significant digit (useful for big-number arithmetic), constructing certain hash functions, and pattern matching algorithms (Burrows-Wheeler transform in compression, suffix array construction). Palindrome detection uses string reversal but production palindrome detectors usually strip non-letters and lowercase before comparing rather than reversing the full string. Surrogate-pair-aware reversal is sufficient for almost all real text; grapheme-aware reversal is needed only when input regularly contains combining marks (Vietnamese, several Indic scripts, certain Arabic forms).

The about text and FAQ on this page were drafted with AI assistance and reviewed by a member of the Coherence Daddy team before publishing. See our Content Policy for editorial standards.

Frequently Asked Questions