Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print text multiple times in Brainfuck

I tried out this hello world program in Brainfuck. How can I print the text multiple number of times? Here's my code:

+++++++[>++++++++++ <- ] >++.>++++++[>++++++++++ <- ] >+++++++++.>+++++++[>++++++++++ <- ] >++++++..>+++++++[>++++++++++ <- ] >+++++++++.>+++[>++++++++++ <-]>++. >++++++++[>++++++++++<-]>+++++++.>+++++++[>++++++++++<-] >+++++++++.>++++++++[>++++++++++ <-]>++.>+++++++[>++++++++++ <- ] >++++++.>++++++[>++++++++++ <-]>++++++++.>+++[>++++++++++<-]>++.>+++[>++++++++++<-]>+++.>+++[>++++++++++<-]>+++.
like image 935
Vishal Varakhedkar Avatar asked Aug 19 '15 15:08

Vishal Varakhedkar


2 Answers

Let's think of a 5 character long word like "hello".

So if you want it to print those 5 characters 3 times you could have a code like this:

,>,>,>,>,>+++[<<<<<.>.>.>.>.>-]

Let me explain the code:

The first part of the code is the input part:

,>,>,>,>,

Then you initialize a variable containing the information that you want to print it 3 times.

>+++

Then you have the loop which goes back to the start, prints out those 5 characters, and goes to the variable and decrement it.

[<<<<< //goes back

.>.>.>.>. //print out

>-]  //decrement

If you got the idea, then you can easily improve the code by e.g. putting more loops in it, I just wanted to show you a simple idea.

like image 93
Leah Avatar answered Dec 12 '22 21:12

Leah


A more general answer is as follows.

Let's say you want to print the text 5 times (we'll call this counter)

+++++ >

and then import characters until a \n (= 10) is given.

----- -----[+++++ +++++ >, ----- -----]

The array now looks like < counter, 0, string >. Finally, we place the pointer at counter.

<[<]<

We finish by printing the string multiple times.

[       while (counter) {
>>[.>]    print string
<[<]<-    counter--
]       }
like image 21
JorisH Avatar answered Dec 12 '22 22:12

JorisH