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.)

Answer from Jaco Van Niekerk on Stack Overflow
🌐
Pixlab
pixlab.io › art
ASCII ART Open Source C Library by PixLab
ASCII Art is a lightweight C/C++ library that converts images or video frames into real-time printable ASCII characters using a single decision tree. Transform visuals into art effortlessly with this efficient and versatile tool.
Top answer
1 of 4
3

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.)

2 of 4
2

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:

  • You can include numbers, special characters (like !, @, #, $, etc), blank space, may be all ASCII chars makes sense.
  • You can support bold, italics, underline and highlight for the characters
  • You can support foreground and background colors
  • You can support animation

Hope this helps!

Discussions

library - ASCII art generator in C - Code Review Stack Exchange
I have written an ASCII art generator library. I was practicing data abstraction and code abstraction and I wanted to know if there is something that can be improved. File tree: | |--Ma... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
November 19, 2015
Convert images to ASCII art
Pretty nice, brief and readable code. However, random remarks: don't remove newlines from command line arguments; they are valid characters in file names (at least on some platforms) there's no need to put the filenames to buffers, can be used as-is there's two checks for output filename, latter one of which is invalid, array is never NULL the resized size calculation causes unnecessarily large error to aspect ratio due to integer division the image is unnecessarily duplicated in case of no resize if you pass 1 as the last argument of stbi_load(), you'll get a monochrome image (less memory, simpler processing) usually r, g and b are not equal in conversion to monochrome strlen(SYMBOLS) could be run for each pixel in the result image (compiler probably replaces it with a build-time constant) some inconsistency in placement of opening curly brace after if and for, choose one style and stick with it :) x and y are not what they usually mean in getintensity() a matter pf taste, but usually if loop variables are coordinates, I personally use x and y instead of i and j image data is over-indexed if channels < 3 More on reddit.com
🌐 r/C_Programming
15
35
July 8, 2023
How do you work with ASCII art in C?
You're looking for Run Length Encoding. There are a few ways to do it, but essentially, the idea is to make repeated characters basically free by placing a count in the sequence when a character is repeated ("encoding" the "length" of the "run"). What it looks like this algorithm does is: --When a single occurrence of a character is encountered, it is printed. --When multiples of the same character are encountered, two are printed (to signal the decoder that a sequence is coming), then a count in plaintext, followed by an asterisk to delimit the count (......, or 6 periods, is turned into ..6*). I'm not entirely sure this is correct, because your output only has one space when it starts the space sequence, but two periods when it starts the period sequence, but you should be able to look at other expected transformations and see the pattern. I personally would use a graduating binary format, turning sequences into something more like e.g. 48 spaces to 0x202030 (3 byte sequence: space,space,0x30), with a key value to use more bytes for the count, but that's not human readable, as many of the representative values would be outside of printable range. Likely your assignment is intended to be readable by people. More on reddit.com
🌐 r/C_Programming
4
0
March 17, 2022
C Ascii Art | Overclockers UK Forums
Is it possible to print ascii art in C? I want the code to print a Man but the character in the man count as control symbols is it possible to get the... More on forums.overclockers.co.uk
🌐 forums.overclockers.co.uk
March 18, 2010
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › cs › csse120 › Projects › AsciiArt › HowToBeginAsciiArt.pdf pdf
Page 1 Ascii Art – Capstone project in C
Do some simple manipulations of the artwork. It will interact with the user by repeatedly displaying (on the console) a numbered menu of choices. The user enters the number for her choice. Then the program does whatever is required for that · choice. This continues until the user selects the QUIT choice. 2. The code that we developed in class today is in your individual repository in a project called ... Note: AsciiArtAgain and NOT AsciiArt.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 161868-how-use-ascii-art-c-cplusplus.html
How to use ASCII Art in C/C++
February 26, 2014 - You could also use xxd if you have linux, unix or OSX (it probably compiles on Windows as well... the source code is a single .c file) ... $ xxd -i asciipic.txt unsigned char asciipic_txt[] = { 0x7c, 0x7c, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, <.... snipped ....> 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x3d, 0x7c, 0x7c, 0x0a }; unsigned int asciipic_txt_len = 1095; Not that this array is not NULL terminated.
🌐
GitHub
github.com › symisc › ascii_art
GitHub - symisc/ascii_art: Real-Time ASCII Art Rendering Library · GitHub
ASCII Art is a single file C/C++ library that let you transform an input image or video frame into printable ASCII characters at real-time using a single decision tree.
Starred by 736 users
Forked by 84 users
Languages   C
Top answer
1 of 4
5
  • fonts/Makefile

    Each object depends only on the corresponding source. It means that header modifications wouldn't trigger recompilation. You may fix it by explicitly spelling out dependencies:

    whimsy.o: whimsy.c whimsy.h core.h
    

    In general it is a good habit to have dependencies auto generated: even in your not very complicated case it is easy to miss the core.h dependency. Take a look at -M family of gcc options.

  • include/whimsy.h

    Do not define objects (like struct font whimsy) in the header file. You never know how many times the client would happen to #include "whimsy.h" in different places of their project. Better practice is to have the definition in the .c file, and declare it in the header as

    extern struct font whimsy;
    
  • DRY?

    Unfortunately, you didn't show your font file. Also, I'm 95% sure the init and print files are identical modulo font name. If I'm correct, you need to unify them, and have the unified version in core.c.

  • Allocation

    Allocating glyphs dynamically and copying them from a static area looks like a waste for me (it would make sense should you read glyphs from the text file instead). I would have a

    struct glyph {
        int width;
        char * appearance;
    };
    

    (strictly speaking, glyph.width is redundant: given a font height and appearance length you may calculate width at runtime); an array of glyphs as a part of struct font:

    struct font {
        ....
        struct glyph typeface[128];
        ....
    }
    

    and a static initialization of each font like

    static struct font whimsy = {
        ....
        .typeface = {
            ....
            ['a'] = (struct glyph) {
                .width = 10,
                .appearance =
                    "          "
                    "          "
                    "          "
                    " d888b8b  "
                    "d8P' ?88  "
                    "88b  ,88b "
                    "`?88P'`88b"
                    "          "
                    "          "
                    "          ";
            },
            ....    
        },
        ....
    };
    

    Beware that such partial array initialization is a gcc extension.

  • More abstraction

    Now you may take advantage of appearance being default initialized to an NULL, and conclude that such glyph is not implemented. An allowed method becomes a trivial test, and is easily abstracted out of the font specifics.

2 of 4
3
  1. Try to format you code properly. Things like these are hard to read:

    struct font{
    unsigned int c;
    unsigned int *r;
    unsigned int d_n;
    int (*allowed)(int);
    int (*index)(int);
    int (*init)(void);
    int (*exit)(void);
    int (*print)(const char*);
    char ***d;
    };
    

    Hard to read code is more difficult to maintain and you're more likely to miss a bug.

  2. Use braces consistently. Things like these just lead to trouble in the long run and don't make the code any easier to read.

    for(int i=0;i<whimsy.d_n;i++)
        for(int j=0;j<whimsy.c;j++)
            free(whimsy.d[i][j]);
    

    Adding additional braces and formatting makes it very explicit what the scope of each loop is and adding additional statements you're less likely to accidentally forget to add a brace.

    for (int i = 0; i < whimsy.d_n; i++) {
        for(int j = 0; j < whimsy.c; j++) {
            free(whimsy.d[i][j]);
        }
     }
    
  3. The use of defines for font_alloc and d_malloc are a pretty bad abuse of the pre-processor. You just use it as an automated copy-n-paste mechanism and copy-n-paste code is bad. #define has it's uses but in this case it's the wrong choice.

Find elsewhere
🌐
GitHub
github.com › MagiHotline › IMGtoASCII
GitHub - MagiHotline/IMGtoASCII: A simple C code that transforms Images to ASCII Art. · GitHub
A simple C code that transforms Images to ASCII Art. - MagiHotline/IMGtoASCII
Author   MagiHotline
🌐
Reddit
reddit.com › r/c_programming › convert images to ascii art
r/C_Programming on Reddit: Convert images to ASCII art
July 8, 2023 -

Hello, I made this program as my first C project. I have some experience programming in Python.

https://github.com/JosefVesely/Image-to-ASCII

I'm looking for advice and criticism. :)

🌐
GitHub
github.com › codewithnick › ascii-art
GitHub - codewithnick/ascii-art: A C++ library to make everyday alphabets look much better on the terminal , this project uses OOPS concepts to make unique fonts and display letters on command line · GitHub
ASCII-ART C++ Library 🚀 This is a C++ library for generating ASCII art in various fonts and styles. It is a port of the popular Python library of the same name. Features 🎉 Supports a variety of fonts, including standard fonts, decorative ...
Starred by 93 users
Forked by 142 users
Languages   C++
🌐
Patorjk
patorjk.com › software › taag
Text to ASCII Art Generator (TAAG)
An online text conversion tool for changing text into ASCII art pictures. The output can be used to decorate emails, online profiles, IMs, and more!
🌐
Reddit
reddit.com › r/c_programming › how do you work with ascii art in c?
r/C_Programming on Reddit: How do you work with ASCII art in C?
March 17, 2022 -

so i was given this assignment and i have to compress and expand ascii work with a code, and i have no clue on how to go on about this, google hasn't helped me much, so i'm just looking for tips on how to go ahead with this assignment.

i'm not sure how to explain this but from

',,,,,,]F                                                8,'

into

',,6*]F 48*8,'

i'm sorry if this doesn't make sense but i'm so confused lol, any articles or examples on how to work on this would be great

🌐
GitHub
gist.github.com › symisc › 8638b3f1959874377547b89d8a8aa801
Introduction to the ASCII Art C/C++ Interfaces - https://pixlab.io/art · GitHub
ASCII Art is a single file, C/C++ library developed by PixLab and based on the work of Nenad Markus that let you transform an input image or video frame into printable ASCII characters at real-time using a single decision tree.
🌐
ASCII Art
asciiart.cc
ASCII Art Generator
A large collection of ASCII art drawings and other related ASCII art pictures.
🌐
Dan
dan.gop › articles › ioccc-ascii-art
IOCCC ascii art • dan.gop
December 1, 2016 - While the contest is very successful ... to the first year of the contest, authors have not only written extremely obfuscated code, but also expressed their programs in the form of ascii art....
🌐
GitHub
github.com › symisc › ascii_art › blob › master › ascii_art.c
ascii_art/ascii_art.c at master · symisc/ascii_art
void AsciiArtRender(ascii_render *pRender, unsigned char *zPixel /*IN/OUT*/, int *pnWidth /*IN/OUT*/, int *pnHeight /*IN/OUT*/, unsigned char *zBuf/* Optional/OUT */, int Optimize)
Author   symisc
🌐
GitHub
github.com › topics › ascii-art
ascii-art · GitHub Topics · GitHub
November 23, 2022 - Android media player library base on FFmpeg 8.0. Support single image frame load, subtitle render, video hw decode and ascii art image filter. ... c linux cli terminal command-line ascii aesthetics ascii-art dvd eyecandy dvd-logo dvd-screensaver cdvd
🌐
Overclockers UK
forums.overclockers.co.uk › software › html, graphics & programming
C Ascii Art | Overclockers UK Forums
March 18, 2010 - printf(".---.\n"); printf("___ /_____\\ \n"); printf("/\\.-`( '.' ) *BANG*"); As you can see the characters used for escape sequences like \ need to be \\ to print as a \ You can do it all on one line by using a \n at an end of line to create a new line. If you want to be adventurous you can stick your art in a file then open the file and print the contents to the console.
🌐
Reddit
reddit.com › r/c_programming › ascii art in c?
r/C_Programming on Reddit: ASCII art in C?
May 28, 2021 -

is there a way to use Ascii Faces like༼ つ ◕_◕ ༽ or ʕ•ᴥ•ʔ in a C code Program? I want to make a simple game that shows these, but the characters are not read properly.
Im just doing stuff like

Printf("ʕ•ᴥ•ʔ");

and i get this xD : ╩òÔÇóß┤ÑÔÇó╩ö

am i doing something wrong?