Generate Ascii art text in C

utsabiem picture utsabiem · Dec 21, 2011 · Viewed 36.5k times · Source

I am trying to generate ascii art text for a fun application. From FIGLET, I got the ASCII pattern. I am using that pattern in a printf statement to print letters. Here is a screenshot of the pattern I got from FIGLET:

FIGLET

Here is the code snippet I use to print A:

printf("      _/_/\n   _/    _/\n  _/_/_/_/\n _/    _/\n_/    _/\n");

Now, I take an input text from user, and show it in ASCII art. As I use printf, I can only generate it vertically:

Vertical image

But I need to do horizontal ASCII art. How to do that ?

Answer

Jaco Van Niekerk picture Jaco Van Niekerk · Dec 21, 2011

Yes, this is a well known problem. The last time I solved this is to use an array for each line and rendering each letter separately.

Firstly, I would represent each letter in an array. For example your A would be something like this:

char* letter[8]; 
letter[0] = "      _/_/ ";
letter[1] = "   _/    _/";
etc.

(Actually a 2D array would be used where each letter is at a different index.)

The actual render would be in an array as well, something along the lines of:

char* render[8];

and then use concatenation to build each line. A simply nested for loop should do the trick:

for each line, i to render (i.e the height of the letters)
    for each letter, j
       concatenate line i of letter j to the to char* i in the array

Finally loop though the array and print each line. Actually, you can skip the render array and simply print each line without a carriage return. Something like the following comes to mind:

for each line, i to render : // (i.e the height of the letters) 
    for each letter, j {
       output line i of letter j
    }
    output a carriage return
}

(My C is a bit rusty, from there the "pseudo" code. However, I think my logic is sound.)