Code patternsDataReverse a string
Reverse a string
Prints a text back to front.
Watch it run
The form “The whole input” works on 10 cells; the named ones are “zero”, “letter” and “end”. Press Play, drag the step slider forwards and back, or edit the input:
The figure needs JavaScript. The program below is the result for the example values.
After the reading loop (see read a line into memory), the pointer stands just past the last letter. One step left is the last letter.
[.<] prints the cell and steps left, until it meets the zero cell before the string. Reversing costs nothing extra: it is the direction the pointer has to walk anyway.
The code
This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:
>,[>,]<[.<]
What it needs
- A zero cell before the string (the read loop starts on cell 1 for that reason).
- Everything else as for read a line into memory: wrapping cells for the newline form, 0 at the end of the input for the other.
Common mistakes
- Text in more than one byte per letter (é, 你): the bytes of each letter come out in the wrong order. The tape holds bytes, not letters.
What it costs
A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:
The figure needs JavaScript. The table below has the measured steps.
Cost as a formula, and measured
| Form | Plain steps | Optimised steps |
|---|---|---|
| The whole input | 5 + 6 per letter | 3 + 4 per letter |
| One line | 15 + 26 per letter | 4 + 6 per letter |
| Clearing up behind it | grows with the letter codes | 3 + 5 per letter |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| The whole input | the input = "" | 8 | 11 | 5 | 3 |
| The whole input | the input = "stressed" | 8 | 11 | 53 | 35 |
| One line | the line = "stressed" | 8 | 41 | 223 | 52 |
| Clearing up behind it | the input = "stressed" | 8 | 14 | 1,815 | 43 |
Other forms
One line
Stops at the newline and does not print it.
>,----------[++++++++++>,----------]<[.<]
Run this form step by step
The form “One line” works on 10 cells; the named ones are “zero”, “letter” and “end”. Press Play, drag the step slider forwards and back, or edit the line:
The figure needs JavaScript. The program below is the result for the example values.
Clearing up behind it
Each letter is erased after it is printed, so the tape ends empty.
>,[>,]<[.[-]<]
Run this form step by step
The form “Clearing up behind it” works on 10 cells; the named ones are “zero”, “letter” and “end”. Press Play, drag the step slider forwards and back, or edit the input:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- A classic first exercise; the challenges ask for it without showing the answer.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "Read all characters into memory", followed by a scan to the left.
Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.