Home>

Read an even number of lines (n is the number of lines) from the standard input, line 0, line n/2, line 1, line (n/2) +1, line 2, I'm creating a program that outputs (n/2) + 2nd line, ... to the standard output in this order, but the following programs are displayed in the order of 02461357.
0
1
2
3
4
5
6
7
Enter
0
4
1
5
2
6
3
7
I want to be

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

#define LINELEN 128
int main (void) {
    char s [LINELEN + 1];
    char * a = NULL;
    char * line [LINELEN + 1];
    int c = 0;
    int i, j;
    while (fgets (s, LINELEN, stdin)! = NULL) {
        a = (char *) malloc (sizeof (char) * (strlen (s) + 1));
        if (a == NULL) {
            fprintf (stderr, "malloc of a failed \ n");
            exit (1);
        }
        strcpy (a, s);
        line [c] = a;
        c ++;
        if (c>LINELEN + 1) {
            break;
        }
    }
    for (i = 0;i<c;i + = 2) {
        printf ("% s", line [i]);
    }
    for (j = 1;j<= c;j + = 2) {
        printf ("% s", line [j]);
    }
    free (a);
    a = NULL;
    return 0;
}
c