C programming: print two rows as two columns next to each other

I have a question regarding a problem with a C program that I am doing. I am going to write two different lines next to each other in two columns. I did not find clear answers to my question, since they almost always give examples of numbers with a known length or quantity.

I have two lines, with a maximum length of 1500 characters, but for me an unknown length. Let for the sake of study these values ​​be given:

char string1[] = "The independent country is not only self-governed nation with own authorities.";
char string2[] = "This status needs the international diplomatic recognition of sovereignty.";

I want to write them next to each other with a column width of twenty characters. I set the difference between the columns to a regular tab. Like this:

The independent coun     This status needs th
try is not only self     e international dipl
-governed nation wit     omatic recognition o
h own authorities.       f sovereignty.

, , , . . .

-, , , , , , c-, for-loops.

void display_columns(char *string1, char *string2);

int main()
{ 
  char string1[] = "The independent country is not only self-governed nation with own authorities.";
  char string2[] = "This status needs the international diplomatic recognition of sovereignty.";

  display_columns(string1,string2);
}

void display_columns(char *string1, char *string2)
{
  int i,j;
  for(i=0;i<5;i++)
  {
     for(j=0+20*i;j<20+20*i;j++)
     {
       printf("%c",string1[j]);
     }

     printf("\t");

     for(j=0+20*i;j<20+20*i;j++)
     {
       printf("%c",string2[j]);
     }
  }
}
+4
2

, .

void print_line(char *str, int *counter) {

    for (int i = 0; i < 20; i++) {
        if (str[*counter] != '\0') {
            printf("%c", str[*counter]);
            *counter += 1;
        } 
        else { printf(" "); }
    }
}

void display_columns(char *string1, char *string2)
{
    int counter = 0, counter2 = 0;

    while (1) {

        print_line(string1, &counter);

        printf("\t");

        print_line(string2, &counter2);

        printf("\n");

        if (string1[counter] == '\0' && string2[counter2] == '\0') {
            break;
        }
    }
}
+3

, :

printf("%c",string1[j]);

putchar(string1[j]);

.

, :

  for(i=0;i<5;i++)
  {
     for(j=0+20*i;j<20+20*i;j++)
     {
       putchar(string1[j]);
     }

     printf("\t");

     for(j=0+20*i;j<20+20*i;j++)
     {
       putchar(string2[j]);
     }
     putchar('\n');
  }

. , :

void display_columns(char *string1, char *string2)
{
  int i,j;
  int len1 = strlen(string1);
  int len2 = strlen(string2);
  int maxlen = (len1 > len2) ? len1 : len2;
  int numloops = (maxlen + 20 - 1) / 20;
  for(i=0; i<numloops; i++)
  {
     for(j=0+20*i;j<20+20*i;j++)
     {
       if (j < len1)
           putchar(string1[j]);
       else
           putchar(' '); // Fill with spaces for correct alignment
     }

     printf("\t");

     for(j=0+20*i;j<20+20*i;j++)
     {
       if (j < len2)
           putchar(string2[j]);
       else
           break; // Just exit from the loop for the right side
     }
     putchar('\n');
  }
}
+3

Source: https://habr.com/ru/post/1616876/


All Articles