Create Ascii Art Text in C

I am trying to create ascii art text for a fun application. With FIGLET, I got the ASCII pattern. I use this template in the instructions printffor printing letters. Here is a screenshot of the template I received from FIGLET:

FIGLET

Here is the code snippet that I use to print A:

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

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

Vertical image

But I need to make horizontal art ASCII. How to do it?

+5
source share
4 answers

Yes, this is a well-known problem. The last time I decided it was to use an array for each line and send each letter every time.

-, . , A :

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

( 2D-, .)

, - :

char * render [8];

. :

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

. , . :

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
}

( C , "" . , , .)

+2

- :

. , , - , , .. , !

#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 */
}

:

> gcc test.c -o print
> print

|       ||-------|       |
|_______||_______|       |
|       ||       |       |
|       ||_______|_______|_______

>

TODO:

  • , (!, @, #, $ ..), , , ASCII .
  • , ,
  • .

, !

+2

, , - , . , , .. , , .

+1

It’s great to see you like Filet. The widget widget that you show does contain a command line tool that generates ASCII output. http://www.figlet.org/ It can also be easily installed using brew http://mxcl.github.com/homebrew/ .

Think, you can easily write your result from your own program to popen(..)or similar.

+1
source

All Articles