1 * 2 ** This program reads input lines from the standard input and prints 3 ** each input line, followed by just some portions of the lines, to 4 ** the standard output. 5 ** 6 ** The first input is a list of column numbers, which ends with a 7 ** negative number. The column numbers are paired and specify 8 ** ranges of columns from the input line that are to be printed. 9 ** For example, 0 3 10 12 -1 indicates that only columns 0 through 3 10 ** and columns 10 through 12 will be printed. 11 */ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #define MAX_COLS 20 /* max # of columns to process */ 17 #define MAX_INPUT 1000 /* max len of input & output lines */ 18 19 int read_column_numbers( int columns[], int max ); 20 void rearrange( char *output, char const *input, 21 int n_columns, int const columns[] ); 22 23 int 24 main( void ) 25 { 26 int n_columns; /* # of columns to process */ 27 int columns[MAX_COLS]; /* the columns to process */ 28 char input[MAX_INPUT]; /* array for input line */ 29 char output[MAX_INPUT]; /* array for output line */ 30 31 /* 32 ** Read the list of column numbers 33 */ 34 n_columns = read_column_numbers( columns, MAX_COLS ); 35 36 /* 37 ** Read, process and print the remaining lines of input. 38 */ 39 while( gets( input ) != NULL ){ 40 printf( "Original input : %s ", input ); 41 rearrange( output, input, n_columns, columns ); 42 printf( "Rearranged line: %s ", output ); 43 } 44 45 return EXIT_SUCCESS; 46 } 47 48 * 49 ** Read the list of column numbers, ignoring any beyond the specified 50 ** maximum. 51 */ 52 int 53 read_column_numbers( int columns[], int max ) 54 { 55 int num = 0; 56 int ch; 57 58 /* 59 ** Get the numbers, stopping at eof or when a number is < 0. 60 */ 61 while( num < max && scanf( "%d", &columns[num] ) == 1 62 && columns[num] >= 0 ) 63 num += 1; 64 65 /* 66 ** Make sure we have an even number of inputs, as they are 67 ** supposed to be paired. 68 */ 69 if( num % 2 != 0 ){ 70 puts( "Last column number is not paired." ); 71 exit( EXIT_FAILURE ); 72 } 73 74 /* 75 ** Discard the rest of the line that contained the final 76 ** number. 77 */ 78 while( (ch = getchar()) != EOF && ch != ' ' ) 79 ; 80 81 return num; 82 } 83 84 * 85 ** Process a line of input by concatenating the characters from 86 ** the indicated columns. The output line is then NUL terminated. 87 */ 88 void 89 rearrange( char *output, char const *input, 90 int n_columns, int const columns[] ) 91 { 92 int col; /* subscript for columns array */ 93 int output_col; /* output column counter */ 94 int len; /* length of input line */ 95 96 len = strlen( input ); 97 output_col = 0; 98 99 /* 100 ** Process each pair of column numbers. 101 */ 102 for( col = 0; col < n_columns; col += 2 ){ 103 int nchars = columns[col + 1] - columns[col] + 1; 104 105 /* 106 ** If the input line isn't this long or the output 107 ** array is full, we're done. 108 */ 109 if( columns[col] >= len || 110 output_col == MAX_INPUT - 1 ) 111 break; 112 113 /* 114 ** If there isn't room in the output array, only copy 115 ** what will fit. 116 */ 117 if( output_col + nchars > MAX_INPUT - 1 ) 118 nchars = MAX_INPUT - output_col - 1; 119 120 /* 121 ** Copy the relevant data. 122 */ 123 strncpy( output + output_col, input + columns[col], 124 nchars ); 125 output_col += nchars; 126 } 127 128 output[output_col] = '