これを解決するための基本的な考え方は、文字列のセグメントの最後のスペースを、そのセグメントの 60 番目の文字より前に追跡することです。
これは宿題なので、コードを考えさせますが、上記の提案の大まかな擬似コードを次に示します。
- current_position = start of the string
- WHILE current_position NOT past the end of the string
- LOOP 1...60 from the current_position (also don't go past the end of the string)
- IF the character at current_position is a space, set the space_position to this position
- Replace the character (the space) at the space_position with a newline
- Set the current_position to the next character after the space_position
- If you're printing the string rather than inserting newline characters into it, you would print any remaining part of the string here.
また、60 文字のブロックにスペースがない場合も考慮してください。