一起打印多行字符串

时间:2014-12-30 00:32:16

标签: c arrays string

我有两个char数组,其中包含1的图片和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";

现在我要打印10(在屏幕上横向翻译大字母)。

我试着用这个:

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

但这不起作用。我该怎么办?

4 个答案:

答案 0 :(得分:3)

如下:

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

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

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

答案 1 :(得分:1)

您不能使用单个printf执行此操作,因为您必须交错。你要做的就是逐行建立它。所以你用\ n分割每个字符串,然后打印

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

答案 2 :(得分:0)

如果您不能按照ouahanswer建议的方式重新设计数据结构 - 这是处理问题的最佳方式(重新构建数据以便于处理通常是一种很好的处理方式) - 然后你需要在你的每个大字符中找到该行的结尾。并依次打印每一行。

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;
}

ab用完数据时,此循环停止。对于样本数组,它们具有相同数量的行,因此没有问题。如果他们有不同数量的线,那么还有更多的工作要做。类似地,代码假定每个数组中的行都具有相同的长度 - 如示例数据中所示。如果它们的长度不同,那么还有更多工作要做。

完整的测试代码

请注意,我在零之后删除了多余的尾随空白。

#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;
}

示例输出

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

答案 3 :(得分:0)

但是你可能不喜欢这个答案,但如果你想要你的&#34; 1&#34;完全打印,然后你的&#34; 0&#34;打印出来,我建议这样:

#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++;

        }
    }