#include <string.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX_WORD_LEN  20
#define MAX_LINE_LEN  50
int IsWhiteSpace(int );
int ReadWord(char *word ) ;
void AddWord(const char *word,char* , int* );
void WriteLine(const char *line,int lineLend ,int numWords);
void ClearLine(char *, int *lineLend,int *numWords);


int main() {
    char word[MAX_WORD_LEN+1];
    int wordLend = 0;
    char line[MAX_LINE_LEN +1];
    int lineLend = 0;
    int numWords = 0;
    //<xóa dòng>
    ClearLine(line,&lineLend,&numWords);
    for(int cnt=0; cnt=100; cnt++) {
        //<Đọc một từ>
        wordLend = ReadWord(word) ;
        printf("%s\n", word);
        //if(Hết từ)
        if (wordLend == 0) {
            if (lineLend>0)
                // in dòng đó ra và thoát
                puts(line);
            return 0;
        }
            /*
            <Từ không vừa dòng hiện tại >
          <In dòng có căn phải>
           <Xóa dòng>
           */
        if((wordLend + 1 + lineLend) > MAX_LINE_LEN) {
            WriteLine(line,lineLend,numWords);
            ClearLine(line,&lineLend,&numWords);
            //fflush(stdin);
        }
         //<Thêm từ vào dòng>
        AddWord(word,line,&lineLend);
        numWords++;
    }
    return 0;
}
// đọc mọt từ
int ReadWord(char *word) {
    int ch; // nếu là char thì không kiểm tra được kí tự EOF = 255
    int pos = 0;

    /*
        + bỏ qua white_space
        + nếu đọc vào kí tự trăng ,tab nó sẽ bỏ qua để đọc kí tự tiếp theo ,xuống dòng
    */
    ch = getchar();
    while(IsWhiteSpace(ch))
        ch = getchar();
    /* Lưu các ký tự vào đến MAX_WORK_LEN. */
    while ((!IsWhiteSpace(ch))&&( ch != 'EOF')) {
        if( pos < MAX_WORD_LEN) {
            word[pos] = (char)ch ;
            pos++;
        }
        ch = getchar();
    }
    word[pos] = '\0';
    // trả vể độ dài từ
    return pos;
}
int IsWhiteSpace(int ch) {
    return (( ch == ' ' )||( ch == '\n')||( ch == '\t'));
}
void AddWord(const char *word, char *line, int *lineLend) {
    if(*lineLend > 0) {
        line[*lineLend] = ' ';
        line[*lineLend + 1] = '\0';
        (*lineLend)++;
    }
    strcat(line,word);
    *lineLend += strlen(word);
}
void WriteLine(const char* line, int lineLend,int numWords) {
    int extraSpaces , spacesToInsert,i,j ;
    //<tính số khoảng tráng dư thừa cho dòng>
    extraSpaces = MAX_LINE_LEN - lineLend ;
    if(extraSpaces == 0)
        puts(line);
    else {
        for(i = 0 ; i < lineLend ; i++) {
            if(line[i] != ' ')
                //<printf charater>
                putchar(line[i]);
            else {
                //<tính số khoảng trống cần bù thêm>
                spacesToInsert = extraSpaces/(numWords - 1);
                //<In 1 space + thêm các space cần bù>
                for(j = 1 ; j <= spacesToInsert + 1; j++)
                    putchar(' ');
                //<Giảm thêm không gian và đếm số từ>
                extraSpaces -= spacesToInsert;
                numWords--;
            }
        }
    }
    putchar('\n');
}
// đang lỗi ở đây:
void ClearLine(char *line, int *lineLend,int *numWords) {
    line[0] = '\0';
    *lineLend = 0;
    *numWords = 0;
}