Question

I have a piece of Python code that reads as follow:

template = \
"""
                 %2s  %2s  %2s

                 %2s   R  %2s

                 %2s  %2s  %2s

    %2s  %2s  %2s   %2s  %2s  %2s   %2s  %2s  %2s    %2s  %2s  %2s

    %2s   B  %2s   %2s   W  %2s   %2s   G  %2s    %2s   Y  %2s

    %2s  %2s  %2s   %2s  %2s  %2s   %2s  %2s  %2s    %2s  %2s  %2s

                 %2s  %2s  %2s

                 %2s   O  %2s

                 %2s  %2s  %2s
    """

print template % tuple(range(1, 49))

I am trying to convert the code above into C (Yes I just started learning, it's probably quite self-evident) and, for the life of me, cannot find any documentation that could help me here.

I have already tried using Cython, but ended up with ridiculously long code that is, for me, not practical to implement in my program. I have also searched Stackoverflow for quite some time, to no avail. If I have missed something, please send me a link.

Was it helpful?

Solution

I can't think of a portable way to generate argument list, so i can't think of any simple solution better than typing the whole list like printf(template, 1, 2, 3, 4, 5, 6, ..., 49).

Another solution would be to manually go through the template and do something like:

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

void print_template_with_range(char *template, int start, int end) {
  int i = 1;
  char *str = strdup(template);
  char *cur = str;
  char *pos;
  while ((pos = strstr(cur, "%2s")) != NULL) {
    *pos = '\0';
    printf("%s", cur);
    printf("%2d", i++);
    cur = pos + 3;
  }
  printf("%s", cur);
  free(str);
}

int main() {
char* format_string = "\n\
             %2s  %2s  %2s\n\
\n\
             %2s   R  %2s\n\
\n\
             %2s  %2s  %2s\n\
\n\
%2s  %2s  %2s   %2s  %2s  %2s   %2s  %2s  %2s    %2s  %2s  %2s\n\
\n\
%2s   B  %2s   %2s   W  %2s   %2s   G  %2s    %2s   Y  %2s\n\
\n\
%2s  %2s  %2s   %2s  %2s  %2s   %2s  %2s  %2s    %2s  %2s  %2s\n\
\n\
             %2s  %2s  %2s\n\
\n\
             %2s   O  %2s\n\
\n\
             %2s  %2s  %2s\n\
    ";
print_template_with_range(format_string, 1, 49);
return 0;
}

Produces:

              1   2   3

              4   R   5

              6   7   8

 9  10  11   12  13  14   15  16  17    18  19  20

21   B  22   23   W  24   25   G  26    27   Y  28

29  30  31   32  33  34   35  36  37    38  39  40

             41  42  43

             44   O  45

             46  47  48

OTHER TIPS

In Python you can throw a collection at a string and magic happens. That's the joy of a high-level language.

C is not joyful or a high-level language.

You're going to have to make an array with the data you want to print and then loop over printf and/or puts calls to emit the output you want (either line-by-line, char-by-char, or a mix).

To do something similar to what you're doing above:

char* format_string = " \
             %2s  %2s  %2s \
\
             %2s   R  %2s \
\
             %2s  %2s  %2s \
\
%2s  %2s  %2s   %2s  %2s  %2s   %2s  %2s  %2s    %2s  %2s  %2s \
\
%2s   B  %2s   %2s   W  %2s   %2s   G  %2s    %2s   Y  %2s \
\
%2s  %2s  %2s   %2s  %2s  %2s   %2s  %2s  %2s    %2s  %2s  %2s \
\
             %2s  %2s  %2s \
\
             %2s   O  %2s \
\
             %2s  %2s  %2s \
    ";

printf(format_string, t[0], t[1], ...);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top