LearnBrainfuck basics

Reading input

By the end of this lesson you can write a program that reads what was typed and answers with it.

The last command

The program here has two commands, a comma and a full stop. Above it is a box in which you type. Type one letter in the box, then press Step twice and watch cell 0:

Calculate with it

One command is added between the two: +. The input is the letter a. Guess what the program prints, then press Play. Afterwards type other letters in the box:

Until there is nothing left

This program reads a character, then loops: it prints the character and reads the next. The input is the word hey. Press Step until the program ends, and watch what the last comma does. The line under the tape says what each comma finds:

Back to front

Write a program that reads two characters and prints them in the opposite order: ab gives ba. It is tried with ab and with one other input:

Check yourself

A free exercise

Read three characters and print only the middle one. Then try: print every character of the input two times. Nothing is checked here:

What you can do now

You can read a character with ,, calculate with it, and copy a whole text with ,[.,]. With this lesson you know all eight commands:

The eight commands of brainfuck.
CommandWhat it does
+ −Add 1 to the cell under the pointer, or take 1 off.
> <Move the pointer one cell to the right or to the left.
.Print the cell as one character.
[ ]Repeat everything between them until the cell under the pointer is zero.
,Read one character of input and put its number in the cell.

The input box sends plain characters. A letter with an accent, or an emoji, is sent as two to four numbers above 127, one per comma: that is how such characters are stored (UTF-8).

To work with what you read, you have to move it around without losing it. Lesson 7 shows how.