CCalclabhub

Text & Writing

How To Reverse Text

Reversing text is the simplest string operation there is, and also one of the easiest to get subtly wrong once the text leaves plain English and starts containing emoji or accented letters.

Quick Answer

reverse(s) = the characters of s in the opposite order

s
The input string
reverse(s)
The same characters in the opposite order
n
The number of characters in the string

The string Reverse this line of text becomes txet fo enil siht esreveR when its characters are reversed, while reversing the words instead gives text of line this Reverse.

What Is How To Reverse Text?

Reversing text means writing its characters in the opposite order, so the last becomes the first and the first becomes the last. The operation is its own inverse: reversing a string twice returns the original.

There are three different things people mean by reversing text, and they give different results. Reversing characters flips the whole string, reversing words keeps each word intact and flips their order, and reversing lines flips the order of the lines.

Reversing characters is the strict version. The word stressed becomes desserts, and stop becomes pots. Because the letters are rearranged rather than reordered as units, the result is often unreadable but occasionally, by luck, forms another real word.

A palindrome is a string that reads the same both ways, so reversing it changes nothing. racecar, level and madam are palindromes, and so is the sentence a man a plan a canal panama once the spaces and capitals are removed.

Reversing words is more useful for text. It produces an anastrophe, the figure of speech Yoda is famous for, and it is also a quick way to check that a list is in the order you think it is.

Reversing lines is a practical editing tool. It turns a chronological list into a reverse-chronological one, and it is the fastest way to check whether the last line of a document is really the last.

The subtlety is that a character is not always what it appears to be. A string is stored as a sequence of code units, and reversing those units blindly can split a character in half.

Emoji are the clearest example. Most emoji sit above the basic multilingual plane and are stored as two code units called a surrogate pair. Reversing the two halves separately produces two broken halves that no longer render as the original symbol.

Accented letters have a related problem. If a letter is stored as a base letter followed by a combining accent mark, reversing the units puts the accent before the letter it belongs to, and the accent may then attach to the wrong character.

The fix is to reverse by code point or by grapheme cluster rather than by code unit. Reversing by code point keeps surrogate pairs together, and reversing by grapheme cluster also keeps combined accents and joined emoji intact.

Reversing is not the same as reversing a file or a byte sequence. Bytes have their own order, and reversing the bytes of a UTF-8 string produces mojibake rather than readable reversed text.

A common use is the mirror or upside-down text seen on novelty sites, where each character is replaced by a look-alike and the order is reversed. That is two operations combined, a substitution and a reversal, not a reversal alone.

The calculator reverses the text by character, word or line, and handles surrogate pairs correctly so that emoji survive the round trip. It reports the character counts of the input and the output.

Formula

reverse(s) = s[n-1] s[n-2] ... s[1] s[0]

Write the characters in the opposite order, last first. Applying the operation twice returns the original string.

SymbolMeaning
s_iCharacter
nLength

s is a palindrome if reverse(s) = s

A string is a palindrome when reversing it produces exactly the same string, which is true for racecar and for a sentence once its spaces and capitals are removed.

SymbolMeaning
sText

How To Calculate How To Reverse Text

  1. 1

    Decide what to reverse

    Characters flip the whole string, words flip the order of the words, and lines flip the order of the lines. They are three different operations.

  2. 2

    Split the text into the right units

    For characters, take the string apart one code point at a time. For words or lines, split on spaces or newlines first.

  3. 3

    Reverse the order of the units

    Walk the list from the end to the beginning and write the units out in that order.

  4. 4

    Reassemble the text

    Join the reversed units back together with the same separators they had before, so word spacing and line breaks are preserved.

  5. 5

    Check for broken characters

    If the text contained emoji or accented letters, confirm they still render. Reversing code units rather than code points is what breaks them.

Examples

Example 1: Check a palindrome

Text
racecar
Mode
Reverse characters
StepCalculationResult
Characterscount the letters7
Reversedwrite the letters in the opposite orderracecar

Result: racecar reversed is racecar, so it is a palindrome: the 7 letters read the same in both directions.

Example 2: Reverse the word order of a sentence

Text
the quick brown fox
Mode
Reverse word order
StepCalculationResult
Wordssplit on the spaces4
Reversed orderwrite the words in the opposite orderfox brown quick the

Result: the quick brown fox becomes fox brown quick the, with the 4 words kept intact but placed in the opposite order.

Calculator

Prefer a full-width tool? Open the How To Reverse Text calculator page.

Common Mistakes

  • Reversing code units instead of code points

    A surrogate pair split down the middle produces two broken halves. Reverse by code point or by grapheme cluster so emoji survive.

  • Separating the accent from its letter

    If an accented letter is stored as a base plus a combining mark, reversing the units moves the mark. Normalise the text first or reverse by grapheme cluster.

  • Expecting the result to be readable

    Reversing characters rearranges letters rather than words, so the output is usually gibberish. Only reversing words or lines tends to stay legible.

  • Forgetting that reversing is its own inverse

    Reversing twice returns the original, so an unexpected result usually means the text was reversed an even number of times.

  • Assuming a reversed palindrome looks the same

    Only the letters read the same. The capitals and spaces do not survive, so a sentence palindrome needs its punctuation stripped before the test.

  • Confusing a reversal with a substitution cipher

    Mirror text replaces each character with a look-alike and then reverses. A plain reversal alone will not produce the mirror effect.

  • Reversing a byte sequence instead of a string

    Reversing the bytes of UTF-8 text breaks every multi-byte character. Reverse characters, not bytes.

FAQ

What is the difference between reversing characters and reversing words?

Reversing characters flips the order of every letter, so hello becomes olleh. Reversing words keeps each word intact and flips the order of the words, so hello world becomes world hello.

What is a palindrome?

A word, phrase or number that reads the same forwards and backwards, such as racecar, level or 12321. Sentence palindromes ignore spaces and punctuation, so they are palindromes of the letters only.

Why did my emoji break when I reversed the text?

Most emoji are stored as two code units. Reversing those units separately splits the emoji in half. Reversing by code point or by grapheme cluster keeps the pair together.

Can I reverse the lines of a document?

Yes, and it is a useful way to convert a chronological list into a reverse-chronological one. Split on the newline character, reverse the list, and join it back with newlines.

Does reversing text change its length?

No. Reversing rearranges the characters without adding or removing any, so the character count is unchanged, provided the reversal is done at the right level.

Is reversed text a form of encryption?

No. A reversal is trivially undone and provides no secrecy. It is an obfuscation at best, useful for puzzles and novelty effects rather than for security.

References

  1. [1]Unicode Consortium, Unicode text segmentation — https://www.unicode.org/reports/tr29/
  2. [2]Encyclopaedia Britannica, Palindrome — https://www.britannica.com/art/palindrome
  3. [3]Unicode Consortium, UTF-16 and surrogate pairs — https://www.unicode.org/faq/utf_bom.html