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:

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:

But I need to do horizontal ASCII art. How to do that ?
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.)
You can try something like the following:
NOTE: Obviously, the following lacks in a lot of things like memory de-allocation, error checking, incomplete code, etc. The idea is to give you a hint!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define ROW 4
#define COL 8
#define CHAR_INDEX_MAX 26
enum alph_enum {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
typedef struct _alphabets {
    char *line[ROW];
}alphabet;
alphabet chars[26];
init_a(enum alph_enum letter)
{
    int i;
    for (i=0 ; i<ROW ; i++) {
        chars[letter].line[i] = (char *) malloc(COL);
        if (chars[letter].line[i] == NULL) {
            printf("memory allocation failed \n");
            return;
        }
    }
    switch (letter) {
                                     /*0123 4567*/
    case H:
        strcpy(chars[letter].line[0], "|       |");
        strcpy(chars[letter].line[1], "|_______|");
        strcpy(chars[letter].line[2], "|       |");
        strcpy(chars[letter].line[3], "|       |");
        break;
    case E:
        strcpy(chars[letter].line[0], "|-------");
        strcpy(chars[letter].line[1], "|_______");
        strcpy(chars[letter].line[2], "|       ");
        strcpy(chars[letter].line[3], "|_______");
        break;
    case L:
        strcpy(chars[letter].line[0], "|       ");
        strcpy(chars[letter].line[1], "|       ");
        strcpy(chars[letter].line[2], "|       ");
        strcpy(chars[letter].line[3], "|_______");
        break;
    /*  for all the other alphabets */
    }
    return;
}
print_str(char word[])
{
    int i, j;
    printf("\n");
    for (i=0; i<ROW; i++) {
        for (j=0 ; j<strlen(word) ; j++) {
            printf("%s", chars[word[j] % 'A'].line[i]);
        }
        printf("\n");
    }
    printf("\n");
    return;
}
int main(void)
{
    init_a(H);
    init_a(E);
    init_a(L);
    /* print_str("HELLO"); */
    print_str("HELL");
    return 0;
    /* free the memory for HEL here */
}
The output will be as follows:
> gcc test.c -o print
> print
|       ||-------|       |
|_______||_______|       |
|       ||       |       |
|       ||_______|_______|_______
>
TODO:
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With