原文代码很老式
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#define MAX_COLUMN 20
#define MAX_LEN 1000
int Read_Column_Number(int *arr,int max);
void ReArranged(char *output,const char *input,const int *arr,const int count);
int main(){
int num_column=0;
int column[MAX_COLUMN]={0};
char input[MAX_LEN]={0};
char output[MAX_LEN]={0};
fputs("Input the column number in pairs:->",stdout);
num_column=Read_Column_Number(column,MAX_COLUMN);
fputs("Input the str:->",stdout);
while(gets(input)!=NULL){
ReArranged(output,input,column,num_column);
printf("New->%s\n",output);
fputs("Input the str:->",stdout);
}
return 0;}
int Read_Column_Number(int *arr,int max){
int count=0,ch=0;
while(1){
for(int i=0;scanf("%d",&arr[i])==1&&arr[i]>=0&&count<max;count++,i++);
if(count%2){
fputs("The number of count must be even,try again!\n",stdout);
while((ch=getchar())!=EOF&&ch!=10);
}
else
break;
}
while((ch=getchar())!=EOF&&ch!=10);
return count;}
void ReArranged(char *output,const char *input,const int *arr,int count){
for(int i=0,num_char=0,len=strlen(input),len_output=0;i<count;i+=2){
if(arr[i]>=len||len_output==MAX_LEN-1)
break;
num_char=arr[i+1]-arr[i]+1;
if(len_output+num_char>MAX_LEN-1)
num_char=MAX_LEN-len_output-1;
strncpy(output+len_output,input+arr[i],num_char);
len_output+=num_char;
}
}
|