# Reversing a String

If you know the length of the string, you can explicitly reverse the order of the characters:

``````Q: concatenation of characters (4; 3; 2; 1; 0) of "abcde"
A: edcba``````

This uses the positions of the characters in reverse order to flip the string. But what if you don’t know how many characters are in the string? There are some properties of strings that can be turned to the task:

``````Q: positions of "abcde"
A: 0
A: 1
A: 2
A: 3
A: 4
A: 5``````

This states that there are six positions in the string (including the pointer to the end of the string), corresponding to the number of characters, plus one. As you scan through the string, there are fewer and fewer characters after the specified position:

``````Q: following texts of (positions of "abcde")
A: abcde
A: bcde
A: cde
A: de
A: e
A: ``````

The length of these strings can be measured, to produce a list of numbers that is a perfect inversion of the positions listed above.

``````Q: lengths of (following texts of (positions of "abcde"))
A: 5
A: 4
A: 3
A: 2
A: 1
A: 0``````

This inverted list can be used to scan in reverse order through the string, concatenating as you go:

``````Q: concatenation of characters (lengths of (following texts of (positions of it)))
of "abcde"
A: edcba``````