#include <stdio.h> #include <stdlib.h> #include <string.h> #define Max_col20#define max_input 1000int Read_column_numbers (int columns[], int max), void rearrange (char *output, char *input, int n_columns, int const columns[]); int main (void) {intn_columns;intcolumns[max_col];charinput[max_input];charoutput[max_input];n_columns = Read_ Column_numbers (columns, max_col); while (gets (input) = NULL) {printf ("Origin Input:%s\n", input); rearrange (output, Input, N_columns, columns);p rintf ("Rearrage output:%s\n", output);} return exit_success;} /* Read the first row format number format */int read_column_numbers (int columns[], int max) {int num = 0;int ch;while (num < max && scanf ("%d ", &columns[num]) = = 1 && columns[num] >= 0) {num++;} if (num% 2 = 0) {puts ("last column number is not paired."); Exit (exit_failure);} while ((ch = getchar ())! = EOF && ch! = ' \ n ') {;} return num;} /* Format input string */void rearrange (char *output, char const *input, int n_columns, int const columns[]) {int OUTPUt_col; /* Output Array counter */int col;/* the subscript of the input character array currently pointed to */int len;/* the length of the input string */len = strlen (input); output_col = 0;for (col = 0; col < N_col Umns; Col + = 2) {int nchars = Columns[col + 1]-columns[col]; /* Number of characters *///when the maximum length or end of the string is reached, stop the loop if (Columns[col] > Len | | output_col = = max_input-1) {break;} When the length exceeds the maximum length, intercept the former part of the string if (Output_col + nchars > max_col-1) {nchars = max_col-output_col-1;} Copy string strncpy (output + output_col, input + Columns[col], nchars);//Offset output string Output_col + = Nchars;} String end Output[output_col] = ' + ';}
Precautions:
1.scanf function scalar parameter front plus & symbol.
2. The function scalar parameter is a pass-through method, and the array name parameter has the semantics of the address invocation.
3. There is no string type, and the string is a character ending with nul bytes.
4. The symbol null is defined in stdio.h, NUL is not defined, and you need to use custom nul as ' \ '.
5. Read the char type with the int type, because char is a small int type and EOF is of type int.
C and Pointers first chapter string processing