How To Reverse Text
📖 Bu rehber ToolPazar ekibi tarafından hazırlanmıştır. Tüm araçlarımız ücretsiz ve reklamsızdır.
Four definitions of “reverse”
Reversing a string is one of the first problems in any programming tutorial, which creates the illusion that it’s trivial. It isn’t. “Reverse this text” can mean reverse every character, reverse every word, reverse the bytes, or reverse the user-perceived characters — and each of those gives a different answer once you include emoji, combining diacritics, or right-to-left scripts. This guide covers the four common reversal definitions, why a naive character-by-character loop breaks modern Unicode text, and how to handle edge cases like flag emoji, Thai vowel marks, and Hebrew. You’ll also learn the palindrome-checking application and the gotchas specific to that.
Character reversal: the naive version
Before writing code, pick a definition:
Why naive reversal breaks on emoji
The textbook approach works for pure ASCII:
Combining characters: the deeper problem
This works until the input has anything beyond the basic multilingual plane.
Grapheme-safe reversal
Even code-point iteration isn’t enough. An “é” can be one code point (U+00E9) or two (U+0065 + U+0301 combining acute). If you reverse the two-code-point form, the accent ends up on the wrong letter.
Word reversal
This handles flag emoji (regional-indicator pairs), skin-tone modifiers, ZWJ sequences (family emoji), and combining marks correctly.
Right-to-left scripts
Word-level reversal flips the order of tokens without reversing each token. “The quick brown fox” becomes “fox brown quick The.”
Byte reversal
Watch the whitespace handling — double spaces, tabs, newlines. Decide whether you want to preserve exact whitespace or normalize.
Palindrome checking
Arabic, Hebrew, and Persian already display right-to-left. Reversing them at the character level produces text that displays in left-to-right order, which looks “forward” to an LTR reader but is actually a scrambled string. Reversing is almost never what you want for RTL content. If you’re rendering a mixed-script sentence, the Unicode bidirectional algorithm handles visual order separately from storage order — don’t fight it.
Line-by-line reversal
Reversing raw UTF-8 bytes produces invalid UTF-8 in almost every case and should be avoided unless you’re doing low-level work on ASCII-only data. The multi-byte continuation bytes will end up in positions where lead bytes belong.
Performance notes
The classic application. Canonical workflow:
Common mistakes
A different kind of “reverse”: keep each line intact but put the last line first. Useful for chronological log files.
Run the numbers
Splitting on empty string and reversing — breaks emoji and combining marks. Reversing then lowercasing palindromes (do it in the other order — case changes in some scripts change the code-point count). Forgetting to normalize before comparing, so “café” and “cafe + combining accent” compare unequal. Expecting meaningful output from reversing RTL text.