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.

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define FIRST_CHAR 'a'
  5. #define LAST_CHAR 'z'
  6. #define QU_MARK (LAST_CHAR+1)
  7.  
  8. // returns a substring of 'str' from index 'begin' (included) to index 'end'
  9. char *subString(char *str, int begin, int end) {
  10. int len = end-begin;
  11. char *sub = malloc((len+1)*sizeof(char));
  12. strncpy(sub, str+begin, len);
  13. sub[len] = '\0';
  14. return sub;
  15. }
  16.  
  17. /* prints the 'l'-th line of the representation of character 'c'
  18. in ASCII-art alphabet 'abc'. 'w' is the width of one character*/
  19. void printChar(int l, char c, char** abc, int w) {
  20. int begin = (c-FIRST_CHAR)*w;
  21. int end = begin+w;
  22. printf("%s", subString(abc[l], begin, end));
  23. }
  24.  
  25. int main(int argc, char** argv)
  26. {
  27. // width and height of the ASCII-art representation of 1 character
  28. int width, height;
  29. scanf("%d\n%d\n", &width, &height);
  30. // text to convert, be careful, fgets store the \n
  31. char text[256];
  32. fgets(text, 256, stdin);
  33. // ASCII art representations of the whole alphabet + ?
  34. char** rows = malloc(height*sizeof(char*));
  35. for (int i = 0; i < height; i++) {
  36. rows[i] = malloc(1024*sizeof(char));
  37. fgets(rows[i], 1024, stdin);
  38. }
  39. // prints the result line by line, character by character
  40. char c;
  41. for (int l = 0; l < height; l++) {
  42. int i = 0;
  43. while ((c = tolower(text[i])) != '\n') {
  44. if (FIRST_CHAR <= c && c <= LAST_CHAR)
  45. printChar(l, c, rows, width);
  46. else
  47. printChar(l, QU_MARK, rows, width);
  48. i++;
  49. }
  50. printf("\n");
  51. }
  52.  
  53. return EXIT_SUCCESS;
  54. }

Java

  1. import java.util.*;
  2.  
  3. class Solution {
  4. static final char FIRST_CHAR = 'a';
  5. static final char LAST_CHAR = 'z';
  6. static final char QU_MARK = LAST_CHAR+1;
  7. /** prints the l-th line of the ASCII-art representation of a character
  8. * @param l line to print
  9. * @param c char to print
  10. * @param rep ASCII-art representation of the whole alphabet + ?
  11. * @param w width of the ASCII-art representation of 1 character
  12. */
  13. public static void printChar(int l, char c, String[] rep, int w) {
  14. int begin = (c-FIRST_CHAR)*w;
  15. int end = begin+w;
  16. System.out.print(rep[l].substring(begin, end));
  17. }
  18.  
  19. public static void main(String args[]) {
  20. Scanner in = new Scanner(System.in);
  21.  
  22. // width and height of the ASCII-art representation of 1 character
  23. int width = in.nextInt();
  24. int height = in.nextInt();
  25.  
  26. // text to convert
  27. in.nextLine();
  28. String text = in.nextLine().toLowerCase();
  29. // ASCII art representations of the whole alphabet + ?
  30. String[] rows = new String[height];
  31. for (int i = 0; i < height; i++)
  32. rows[i] = in.nextLine();
  33. // prints the result line by line, character by character
  34. for (int l = 0; l < height; l++) {
  35. for (char c : text.toCharArray())
  36. if (FIRST_CHAR <= c && c <= LAST_CHAR)
  37. printChar(l, c, rows, width);
  38. else
  39. printChar(l, QU_MARK, rows, width);
  40. System.out.print("\n");
  41. }
  42. }
  43. }

Python 3

  1. # some constants, ord gives the ascii code of a character (chr does the opposite)
  2. FIRST_CHAR = ord('a')
  3. LAST_CHAR = ord('z')
  4. QU_MARK = LAST_CHAR+1
  5.  
  6. def printChar(l, c, rep, w):
  7. ''' prints the 'l'-th line of the ASCII-art representation of a character 'c'
  8. rep is the ASCII-art representation of the whole alphabet + ?
  9. and w width of the ASCII-art representation of 1 character'''
  10. begin = (ord(c)-FIRST_CHAR)*w
  11. end = begin+w
  12. print(rep[l][begin:end], end="")
  13.  
  14. # width and height of the ASCII-art representation of 1 character
  15. # text to convert, ASCII art representations of the whole alphabet + ?
  16. width, height, text, rows = int(input()), int(input()), input().lower(), []
  17. for i in range(height):
  18. rows.append(input())
  19.  
  20. # prints the result line by line, character by character
  21. for l in range(height):
  22. for c in text:
  23. if (FIRST_CHAR <= ord(c)) and (ord(c) <= LAST_CHAR):
  24. printChar(l, c, rows, width)
  25. else:
  26. printChar(l, chr(QU_MARK), rows, width)
  27. print()