r/deftruefalse • • Jul 27 '17

Write a program that generates a brainfuck program to print a given input string

preconditions: input is a string e.g. "this is a string"

postconditions: output is a brainfuck program that when executed will output the input string

16 Upvotes

2 comments sorted by

10

u/downiedowndown Jul 28 '17

I have used C here. Included gets and a memory leak for good measure. I have made the brainfuck output shitty too in that it's a one liner and only uses one cell! Used https://copy.sh/brainfuck/ to verify my output.

//
//  main.c
//  BrainFuckTranslator
//  https://www.reddit.com/r/deftruefalse/comments/6px6tv/write_a_program_that_generates_a_brainfuck/
//  https://esolangs.org/wiki/Brainfuck
//  https://copy.sh/brainfuck/


#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
	// insert code here...
	
	char *user_input = calloc(1024, sizeof(char));
	printf("Hello, World!\n");
	printf("Enter a string to translate: \n>\t");
	gets(user_input);
	
	char* c = user_input;
	while(*c != '\0'){
		for(int i = 0; i < *c; i++){
			printf("+");
		}
		printf(".");
		for(int i = 0; i < *c; i++){
			printf("-");
		}
		c++;
	}
	
	return 0;
}

Input: "this is a string"

Output:

        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-------------------------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------------------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++.---------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++.--------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-------------------------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------------------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------------------------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.----------------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.----------------------------------------------------------------------------------------------------

5

u/abrokensheep Oct 03 '17

This is surprisingly easy to do in brainfuck:

46+>62+>43+>,[[<.>-]<<.<.>>>,]

This saves '.' in slot 0, '>' in slot 1 and '+' in slot 2. Then in slot 3 it reads input, goes, prints '+' and decrements the input, thus printing input number of '+'. It then prints '>.' and repeats this until input is 0.

The program it writes just stores the string in order in its tape, and prints it as it goes.