简体   繁体   中英

Printing multi-line strings together

I have two char arrays, containing an image for 1 and an image for 0 .

char a[] =  "      &&\n"    
            "     & &\n"
            "    &  &\n"
            "   &   &\n"
            "  &    &\n"
            " &     &\n"
            "&      &\n"
            "       &\n"
            "       &\n"
            "       &\n"
            "       &\n"
            "       &\n"
            "       &\n";

char  b[] = "    & & & &    \n"
            "   &       &   \n"
            "  &         &  \n"
            " &           & \n"
            "&             &\n"
            "&             &\n"
            "&             &\n" 
            "&             &\n"
            "&             &\n"
            " &           & \n"
            "  &         &  \n"
            "   &       &   \n"
            "    & & & &    \n";

Now I want to print 10 (in big letters horizontally across the screen).

I tried to use this:

printf("%s  %s", a, b);

but this didn't work. What should I have done?

What about something like:

char *a[] = { "      &&",
              "     & &",
              "    &  &",
              "   &   &",
              "  &    &",
              " &     &",
              "&      &",
              "       &",
              "       &",
              "       &",
              "       &",
              "       &",
              "       &"};

char *b[] = /* ... */; 

for (size_t i = 0; i < sizeof a / sizeof *a; i++)
{
    printf("%s %s\n", a[i], b[i]);    
}

You cannot do this with a single printf as you have to interleave. What you have to do is to build it line by line. So you split each string by \\n and then print

for (int i=0;i<sizeof lines_a / sizeof char*;i++)
    printf("%s %s\n", lines_a[i], lines_b[i]);

If you can't redesign your data structure the way ouah suggests in his answer — which is the best way to deal with the problem (restructure the data to make it easy to process is very often a good way of proceeding) — then you need to find the end of the line in each of your 'big characters' and print each line in turn.

char *a_line = a;
char *b_line = b;
char *a_end;
char *b_end;

while ((a_end = strchr(a_line, '\n')) != 0 &&
       (b_end = strchr(b_line, '\n')) != 0)
{
    int a_len = a_end - a_line;
    int b_len = b_end - b_line;
    printf("%.*s   %.*s\n", a_len, a_line, b_len, b_line);
    a_line = a_end + 1;
    b_line = b_end + 1;
}

This loop stops when either a or b runs out of data. With the sample arrays, they have the same number of lines, so there's no problem. If they had different numbers of lines, there'd be more work to do, too. Similarly, the code assumes that the lines in each array are all the same length — as in the sample data. If they were of different lengths, there'd be more work to do.

Complete test code

Note that I've removed the superfluous trailing blanks after the zero.

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

int main(void)
{
    char a[] =  "      &&\n"    
                "     & &\n"
                "    &  &\n"
                "   &   &\n"
                "  &    &\n"
                " &     &\n"
                "&      &\n"
                "       &\n"
                "       &\n"
                "       &\n"
                "       &\n"
                "       &\n"
                "       &\n";

    char  b[] = "    & & & &    \n"
                "   &       &   \n"
                "  &         &  \n"
                " &           & \n"
                "&             &\n"
                "&             &\n"
                "&             &\n" 
                "&             &\n"
                "&             &\n"
                " &           & \n"
                "  &         &  \n"
                "   &       &   \n"
                "    & & & &    \n";

    char *a_line = a;
    char *b_line = b;
    char *a_end;
    char *b_end;

    while ((a_end = strchr(a_line, '\n')) != 0 &&
           (b_end = strchr(b_line, '\n')) != 0)
    {
        int a_len = a_end - a_line;
        int b_len = b_end - b_line;
        printf("%.*s   %.*s\n", a_len, a_line, b_len, b_line);
        a_line = a_end + 1;
        b_line = b_end + 1;
    }

    return 0;
}

Sample output

      &&       & & & &    
     & &      &       &   
    &  &     &         &  
   &   &    &           & 
  &    &   &             &
 &     &   &             &
&      &   &             &
       &   &             &
       &   &             &
       &    &           & 
       &     &         &  
       &      &       &   
       &       & & & &    

However you may not like this answer,but If you want your "1" is printed completely and then your "0" is printed, I suggest this way:

#include<stdio.h>
#include<Windows.h>
//gotoxy sets the cursor in position of int x,int y
void gotoxy(int x , int y){
   COORD newPosition={x,y};
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),newPosition);
}
//getxy gets current position of cursor and returns a COORD(a struct with fields of X and Y)
   COORD getxy(void){
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ),&csbi);
   return csbi.dwCursorPosition;
}
int main(){
        char a[] =  "      &&\n"    
                    "     & &\n"
                    "    &  &\n"
                    "   &   &\n"
                    "  &    &\n"
                    " &     &\n"
                    "&      &\n"
                    "       &\n"
                    "       &\n"
                    "       &\n"
                    "       &\n"
                    "       &\n"
                    "       &";//I deleted '\n'

        char  b[] = "    & & & &    $"
                    "   &       &   $"
                    "  &         &  $"
                    " &           & $"
                    "&             &$"
                    "&             &$"
                    "&             &$" 
                    "&             &$"
                    "&             &$"
                    " &           & $"
                    "  &         &  $"
                    "   &       &   $"
                    "    & & & &    $";
        //your codes
        COORD first=getxy();//get position of first member of your "1"
        printf("%s",a);
        COORD last=getxy();//get position of last member of your "1"
        gotoxy(last.X+1,first.Y);
        COORD current=getxy();
        int i=0;
        while(b[i]!='\0'){
            if (b[i]=='$'){//this if statement does like '\n' but sets the cursor in your defined first_of_the_line
                gotoxy(current.X,current.Y+1);//current.X is your defined first_of_the_line
                current=getxy();
            }
            else
                printf("%c",b[i]);
            i++;

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM