感觉自己弄出来了。。
自己好蠢。。//Qi Ji
//student No. 14209913
#include<stdio.h>
#include<string.h>
int readline(char sentence[1024]);
int countgaps(char sentence[1024],int length);
void display(char sentence[1024],int length,int gaps);
int length1;
int length2;
int gap1;
int gap2;
int main(){
char sentence1[1024];
char sentence2[1024];
while(1){
printf("Enter first line of text:");
gets(sentence1);
length1=readline(sentence1);
if(length1!=-1){
break;}
}
while(1){
printf("Enter second line of text:");
gets(sentence2);
length2=readline(sentence2);
if(length2!=-1){
break;}
}
//printf("%d,%d,",length1,length2);
gap1=countgaps(sentence1,length1);
gap2=countgaps(sentence2,length2);
//printf("%d,%d\n",gap1,gap2);
display(sentence1,length1,gap1);
printf("\n");
display(sentence2,length2,gap2);
printf("\n");
}
int readline( char sentence[1024]){
int len;
len=strlen(sentence);
if(len>50){
printf("Error! The text is longer than the column.");
len=-1;
} else{
len=strlen(sentence);
}
return len;
}
int countgaps(char sentence[1024],int length){
int gaps;
int i;
gaps=0;
for(i=0;i<length;i++){
if(sentence[i]==' '){
gaps++;
}
}
return gaps;
}
void display(char sentence[1024],int length,int gaps){
int i;int extra;int extraspace;int extraadd;
extra=50-length;
if(gaps==0){ //avoid gaps=0,then extra/0 cause error!
extraspace = -1;
extraadd=-1;}
else{
extraspace=extra/gaps;//
extraadd=extra%gaps; //
}
int j;
for(i=0;i<50;i++){
printf("%c",sentence[i]);
if(sentence[i]==' '){
for(j=0;j<extraspace;j++){
printf(" ");
}
if(extraadd>0){
printf(" ");
extraadd--;
}
}
}
}
|