/*
    第2種情報処理技術者試験    平成11年度・秋期・午後 問11(設問1)
                                BohYoh Shibata PREPARATION
*/

#include <stdio.h>
#include <string.h>
#define  BUFFMAX  1024
#define  WORDMAX  16

void wordwrap(char str[], int max)
{
    char word[BUFFMAX / 2][WORDMAX], buff[BUFFMAX];
    int  leng[BUFFMAX / 2], i, idx, cnt, pos;
                         /*** 文字列を単語に分解する ***/
    i = idx = cnt = 0;
    while (str[i] != '\0') {
        if (str[i] == ' ') {
            word[idx][cnt] = '\0';
            leng[idx] = cnt;
            idx++;
            cnt = 0;
        }
        else {
            word[idx][cnt] = str[i];
            cnt++;
        }
        i++;
    }
    word[idx][cnt] = str[i];
    leng[idx] = cnt;
                         /*** 出力する ***/
    i = pos = 0;
    while (i <= idx) {
        if ((pos + leng[i]) > max) {
            buff[pos-1] = '\0';
            printf("%s\n", buff);
            pos = 0;
        }
        strcpy(&buff[pos], word[i]);
        pos += leng[i];
        buff[pos] = ' ';
        pos++;
        i++;
    }
    buff[pos-1] = '\0';
    printf("%s\n", buff);
}