Program 1.1 rearrange. c, 1.1rearrange.c

Source: Internet
Author: User

Program 1.1 rearrange. c, 1.1rearrange.c

1/* 2 ** this program reads the input line from the standard input (keyboard) and processes it as needed and prints it in the standard output (screen, 3 ** the next line of each input row is the output content of the row processed as needed. 4 ** 5 ** the input 1st rows are a series of column numbers, ending with a negative number at the end of the string. 6 ** these column labels appear in pairs, indicating the columns of the input row to be printed. 7 ** for example, 0 3 10 12-1 indicates that columns 0th to 3rd are printed. 8 */9 10 # include <stdio. h> 11 # include <stdlib. h> 12 # include <string. h> 13 # maximum number of column labels processed by define MAX_COLS 20/*/14 # Maximum length of input rows processed by define MAX_INPUT 1000/*/15 16 int read_column_numbers (int columns [], int max); 17 void rearrange (char * output, char const * input, int n_columns, int const columns []); 18 19 int main (void) 20 {21 int n_columns; /* record Jun: Number of actually read Column labels */22 int columns [MAX_COLS];/* Capacity Ji Jun: Actually read Column numbers */23 char input [MAX_INPUT];/* container Jun: input line string */24 char output [MAX_INPUT];/* container Jun: output line string */25 26/* 27 ** read the column number of this string and calculate the number of 28 */29 n_columns = read_column_numbers (columns, MAX_COLS ); 30 31/* 32 ** read, process, and print the remaining input rows 33 */34 while (gets (input )! = NULL) 35 {36 printf ("original input: % s \ n", input); 37 rearrange (output, input, n_columns, columns); 38 printf ("truncated output: % s \ n ", output); 39} 40 41 return EXIT_SUCCESS; 42} 43 44/* 45 ** read Column label (saved to array), out of the specified range (2) the 46 ** 47 ** parameter table is ignored: columns column labels are stored with the array 48 ** max array maximum capacity 49 ** 50 ** returned value: num actually reads the number of column labels 51 */52 int read_column_numbers (int columns [], int max) 53 {54 int num = 0;/* count Jun: number of currently read Column numbers */55 int end_c H;/* record Jun: End character */56 57/* 58 ** Number of read columns, if the number of reads is less than 0, stop 59 */60 while (num <max & scanf ("% d", & columns [num]) = 1 & columns [num]> = 0) 61 {62 num + = 1; 63} 64 65/* 66 ** confirm that the read number is an even number, because they appear in the format of 67 */68 if (num % 2! = 0) 69 {70 puts ("Last column number is not supported Red. "); 71 exit (EXIT_FAILURE); 72} 73 74/* 75 ** discard the column number that exceeds the limit, prevent being interpreted as 1st rows of Data 76 */77 while (end_ch = getchar ())! = EOF & end_ch! = '\ N') 78; 79 80 return num; 81} 82 83/* 84 ** processes the input row and Concatenates the characters in the specified column, output rows end with NUL 85 */86 void rearrange (char * output, char const * input, int n_columns, int const columns []) 87 {88 int I; /* counting Jun: subscript of the columns array */89 int len;/* record Jun: length of the input row */90 int out_emp_ I;/* counting Jun: the subscript at the beginning of the output array space */91 92 len = strlen (input); 93 94 out_emp_ I = 0;/* bug: forgot to initialize */95 96/* 97 ** to process each pair of column numbers 98 */99 for (I = 0; I <n_columns; I ++ = 2) 100 {101 int nchars = columns [I + 1]-columns [I] + 1; /* number of characters written and output */102/103/* 104 ** if the input row ends or the output row array is full, end the task 105 */106 if (columns [I]> = len | out_emp_ I = MAX_INPUT-1) 107 {108 break; 109} 110 111/* 112 ** if the data space of the output row is insufficient, only 113 of the data that can be accommodated can be copied */114 if (out_emp_ I + nchars> MAX_INPUT-1) 115 {116 nchars = MAX_INPUT-1-out_emp_ I; 117} 118 119/* 120 ** copy related data (like pointer offset usage) 121 */122 strncpy (output + out_emp_ I, input + columns [I], nchars); 123 out_emp_ I + = nchars; 124} 125 output [out_emp_ I] = '\ 0 '; 126}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.