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:
Notice that after the first step cell 0 held a number, the number of your letter, and that the second step printed the letter again. A small a is 97, as in the chart of Lesson 3.
The first command, the comma ,, takes the next character of the text and puts its number in the cell under the pointer. Whatever the cell held before is gone. The text a program is given to read is its input; on these pages you type all of it before the program starts. The comma was the eighth and last command of brainfuck.
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:
Notice that the letter was only a number once it was in the cell, and that the program treated it like any other: add one and you get the next character of the table of Lesson 3.
Try z, and try 9. You get { and a colon: the program knows nothing about alphabets or digits. It only adds one.
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:
Notice that after the third character there was nothing left to read, so the last comma put 0 in the cell. Zero is exactly what ends a loop.
The program ,[.,] is known as cat, after an old command that copies its input. Nearly every program that reads text has this shape: read once before the loop, and read again as the last thing inside it.
What a comma does when the input has run out differs between brainfuck machines: 0 here, but some give 255 and some leave the cell as it was. Every one of the Challenges states its rule, and the Reference explains the three.
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:
Hint
You need both characters at the same time, so they cannot share a cell.
Hint
Read into cell 0, step right, read into cell 1. Then print cell 1 first.
,>,.<. Two cells, two characters, and you choose the order of the visits. A second comma on the same cell would have wiped out the first character.
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:
| Command | What 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).