假设我们有一个声明的字符串......
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宽度之后有空格,我想要包装文本。
有人可以提供代码或任何帮助来创建这样的东西。
答案 0 :(得分: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个字符的块中没有空格的情况。