CCalclabhub

Text & Writing

Reversing a string

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

Last updated

The 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

What Each Symbol Means

s_icharacter

Character

The i-th character of the string.

ncharacters

Length

How many characters the string has.

See It Worked With Real Numbers

A formula on its own does not tell you which number goes where. The how to reverse text page applies this equation to a concrete case, shows every intermediate step, and names the mistakes that produce a wrong answer even when the algebra is right.

Where This Formula Appears

This equation is the working core of How To Reverse Text, filed under Everyday Life. Every number the guide produces comes from substituting into this one line.

Common Questions

What is the formula for Reversing a string?

The formula for Reversing a string is 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.

What units should I use with Reversing a string?

Substitute s_i in character, n in characters. Convert every input to those units before you start, because the equation only returns the right answer when the units on both sides agree.

What is a common mistake when using Reversing a string?

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.