ASCII art


The aim of this puzzle is to copy and paste part of strings given in input, to convert a simple string into a nice ASCII-art representation. Once you have a few auxiliary functions to select the right parts of the strings, it is quite easy.

C

More dynamic allocation, string manipulation. Note the usage of preprocessor macros to make the code easyly maintainable : in a few keystrokes it would be possible to change the number of letters in the alphabet.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define FIRST_CHAR 'a'
#define LAST_CHAR 'z'
#define QU_MARK (LAST_CHAR+1)

// returns a substring of 'str' from index 'begin' (included) to index 'end' 
char *subString(char *str, int begin, int end) {
    int len = end-begin;
    char *sub = malloc((len+1)*sizeof(char));
    strncpy(sub, str+begin, len);
    sub[len] = '\0';
    return sub;
}

/* prints the 'l'-th line of the representation of character 'c'
   in ASCII-art alphabet 'abc'. 'w' is the width of one character*/
void printChar(int l, char c, char** abc, int w) {
    int begin = (c-FIRST_CHAR)*w;
    int end = begin+w;
    printf("%s", subString(abc[l], begin, end));
}

int main(int argc, char** argv)
{
    // width and height of the ASCII-art representation of 1 character
    int width, height;
    scanf("%d\n%d\n", &width, &height);
    
    // text to convert, be careful, fgets store the \n
    char text[256];
    fgets(text, 256, stdin);
    
    // ASCII art representations of the whole alphabet + ?
    char** rows = malloc(height*sizeof(char*));
    for (int i = 0; i < height; i++) {
        rows[i] = malloc(1024*sizeof(char));
        fgets(rows[i], 1024, stdin);
    }
    
    // prints the result line by line, character by character
    char c;
    for (int l = 0; l < height; l++) {
        int i = 0;
        while ((c = tolower(text[i])) != '\n') {
            if (FIRST_CHAR <= c && c <= LAST_CHAR)
                printChar(l, c, rows, width);
            else
                printChar(l, QU_MARK, rows, width);
            i++;
        }
        printf("\n");
    }

    return EXIT_SUCCESS;
}
            

Java

import java.util.*;

class Solution {
    static final char FIRST_CHAR = 'a'; 
    static final char LAST_CHAR = 'z';
    static final char QU_MARK = LAST_CHAR+1;
    
    /** prints the l-th line of the ASCII-art representation of a character
      * @param l line to print
      * @param c char to print
      * @param rep ASCII-art representation of the whole alphabet + ?
      * @param w width of the ASCII-art representation of 1 character
      */
    public static void printChar(int l, char c, String[] rep, int w) {
        int begin = (c-FIRST_CHAR)*w;
        int end = begin+w;
        System.out.print(rep[l].substring(begin, end));
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);

        // width and height of the ASCII-art representation of 1 character
        int width = in.nextInt();
        int height = in.nextInt();

        // text to convert
        in.nextLine();
        String text = in.nextLine().toLowerCase();
        
        // ASCII art representations of the whole alphabet + ?
        String[] rows = new String[height];
        for (int i = 0; i < height; i++)
            rows[i] = in.nextLine();
        
        // prints the result line by line, character by character
        for (int l = 0; l < height; l++) {
            for (char c : text.toCharArray())
                if (FIRST_CHAR <= c && c <= LAST_CHAR)
                    printChar(l, c, rows, width);
                else
                    printChar(l, QU_MARK, rows, width);
            System.out.print("\n");
        }
    }
}            

Python 3

# some constants, ord gives the ascii code of a character (chr does the opposite)
FIRST_CHAR = ord('a')
LAST_CHAR = ord('z')
QU_MARK = LAST_CHAR+1

def printChar(l, c, rep, w):
    ''' prints the 'l'-th line of the ASCII-art representation of a character 'c'
        rep is the ASCII-art representation of the whole alphabet + ?
        and w width of the ASCII-art representation of 1 character'''
    begin = (ord(c)-FIRST_CHAR)*w
    end = begin+w
    print(rep[l][begin:end], end="")

# width and height of the ASCII-art representation of 1 character
# text to convert, ASCII art representations of the whole alphabet + ?
width, height, text, rows = int(input()), int(input()), input().lower(), []
for i in range(height):
    rows.append(input())

# prints the result line by line, character by character
for l in range(height):
    for c in text:
        if (FIRST_CHAR <= ord(c)) and (ord(c) <= LAST_CHAR):
            printChar(l, c, rows, width)
        else:
            printChar(l, chr(QU_MARK), rows, width)
    print()