Coding Solution

ASCII Art – CodinGame solution in C

Easystring slicingcharacter codesindex arithmetic2D outputUpdated
Short answer

Convert each character of the text to an index: 0-25 for letters (case-insensitive) and 26 for anything else, which is drawn as "?". Then, for each of the H rows of the font, print the slice row[index*L : index*L + L] for every character, and end the row with a newline. The output is built row by row, not letter by letter.

The problem

You receive a letter width L, a letter height H, a line of text T, and then H lines that together draw the 27 glyphs A to Z followed by "?", side by side. Each glyph is exactly L characters wide inside those rows.

Your program must print the text T in this ASCII-art font, H lines tall. Lowercase and uppercase letters use the same glyph, and any character that is not a letter (digits, spaces, punctuation) is drawn with the "?" glyph.

Approach, step by step

  1. Read the font rows exactly as givenRead L and H, then the text, then H full lines. Do not trim or split the font rows: leading and trailing spaces are part of the glyphs.
  2. Map every character to a glyph indexUppercase the character; if it is between A and Z its index is c - 'A', otherwise it is 26, the position of "?". Doing this once per character keeps the drawing loop simple.
  3. Draw row by rowThe terminal prints top to bottom, so the outer loop runs over the H rows and the inner loop over the characters of T. For each character, append the substring of length L starting at index * L.
  4. Finish each row with a newlinePrint the assembled row followed by a newline. Keep the trailing spaces that come from the font: copying the slices verbatim is the safest way to match the expected output exactly.

Complexity

TimeO(H × N × L)
SpaceO(H × 27 × L)

Every output character is copied once from the font, and the output size is H × N × L. Memory is dominated by the font rows themselves.

Solution code

Bug fixed in this version: The C version uses tolower() without #include <ctype.h>; recent GCC versions treat implicit function declarations as an error, so it may not compile. It also mallocs a new substring for every printed slice without freeing it, and the format "%d\n%d\n" makes scanf skip all whitespace after H, so a text that starts with spaces loses them. The replacement reads lines with fgets, prints slices with printf("%.*s") and needs no allocation.

ASCII Art – C

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

int main()
{
    char buf[1100];
    fgets(buf, sizeof buf, stdin);
    int L = atoi(buf);
    fgets(buf, sizeof buf, stdin);
    int H = atoi(buf);

    char text[300];
    fgets(text, sizeof text, stdin);
    text[strcspn(text, "\r\n")] = '\0';

    static char rows[32][1100];
    for (int i = 0; i < H; i++)
        fgets(rows[i], sizeof rows[i], stdin);

    for (int r = 0; r < H; r++) {
        for (int i = 0; text[i]; i++) {
            int c = toupper((unsigned char)text[i]);
            int idx = (c >= 'A' && c <= 'Z') ? c - 'A' : 26;
            printf("%.*s", L, rows[r] + idx * L);
        }
        printf("\n");
    }
    return 0;
}

Common mistakes

Going further

The puzzle is really about index arithmetic on a flattened 2D array: the font is a grid of H rows by 27·L columns, and glyph k occupies columns k·L to k·L + L - 1. Once you see it that way, there is nothing to "parse" in the drawing: you never need to split the font into separate letter objects. A nice consequence is that the same code works for any font size, which is exactly what the hidden tests check with different L and H values. If your output looks right in the console but fails, compare character counts: every output row must be exactly N·L characters long, and a single trimmed space or a missing "?" for a digit is enough to break that invariant.

FAQ

How do I handle characters that are not letters in ASCII Art?

Anything outside A-Z after converting to uppercase, including spaces and digits, uses the 27th glyph, the "?" at index 26.

Why is my ASCII Art output shifted?

Usually the font rows were trimmed or split, which removes meaningful spaces. Keep each row exactly as read and slice it at index * L.

Should I print letter by letter or row by row?

Row by row. The console prints lines from top to bottom, so for each of the H rows you print the corresponding slice of every character of the text.

Similar puzzles

★ 5.0 · 16 reviews · 928 users Add to Chrome