1

文字列が宣言されているとしましょう...

string paragraphy = "This is a really really long string containing a paragraph long content. I want to wrap this text by using a for loop to do so.";

この文字列変数を使用して、テキストが60幅を超えている場合、および60幅の後にスペースがある場合は、テキストを折り返したいと思います。

誰かが私にこのようなものを作成するためのコードまたは助けを提供してくれませんか?

4

1 に答える 1

0

これを解決するための基本的な考え方は、文字列のセグメントの最後のスペースを、そのセグメントの 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 文字のブロックにスペースがない場合も考慮してください。

于 2012-04-05T04:19:37.733 に答える