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;
}
-
Answer # 1
-
Answer # 2
The lower half is displayed while the upper half is displayed.
The free process was also wrong.# include
#include #include #define LINELEN 128 int main (void) { char s [LINELEN + 1]; char * line [LINELEN + 1]; int c = 0; for (;c
Related articles
- c - i don't know how to write a shellsort
- how much do you check with unit tests?
- i don't know how to count
- c - i don't know how to handle strings
- c - how to handle up to 79 digits
- program for transposing m rows and n columns
- c - how to use the for statement
- how to use strcpy and strcat
- how to use char
- c - how to save as mplab x ide
- how to load your own library with clion(cmakelisttxt)?
- remove rows and columns of specified numbers in n×n matrix
- how to install stm32cubemx on mac
- c - i don't know how to do eof
- how to speed up edit distance
- c - how to use the quadruple loop
- vim how to get out of brackets
- how to use fscanf and what to put in () of rewind()?
- i want to find the number of columns where the maximum value of the column components is even
- javascript - i don't know how to replace documentwrite
Trends
The current code is output every 2 lines
First, let's write code that outputs half of the total number of lines in sequence.