Here is the code
Code:
std::vector<std::string> StringUtils::WordWrap(const std::string & words, size_t line_length)
{
std::vector<std::string> temp;
std::string tempWords = words;
bool fullWord = false;
std::string::size_type i = 0, str_len = words.length();
while (i < str_len)
{
// skip white space
while (i<str_len && (tempWords[i] == ' ')) { ++i; }
std::string::size_type j = i;
while (j<str_len && j != line_length + i ) { ++j; }
while (tempWords[j] != ' ' && j != str_len)
{
j--;
}
// if we've actually a word ...
if (i != j) {
temp.push_back(words.substr(i, j - i));
}
i = j;
}
return temp;
}
There are 23 lines wrapped at 20 characters:
Line # 1 [18] |When in the Course|
Line # 2 [19] |of human events, it|
Line # 3 [17] |becomes necessary|
Line # 4 [17] |for one people to|
Line # 5 [12] |dissolve the|
Line # 6 [15] |political bands|
Line # 7 [20] |which have connected|
Line # 8 [18] |them with another,|
Line # 9 [19] |and to assume among|
Line # 10 [17] |the powers of the|
Line # 11 [19] |earth, the separate|
Line # 12 [20] |and equal station to|
Line # 13 [17] |which the Laws of|
Line # 14 [13] |Nature and of|
Line # 15 [20] |Nature's God entitle|
Line # 16 [14] |them, a decent|
Line # 17 [14] |respect to the|
Line # 18 [19] |opinions of mankind|
Line # 19 [18] |requires that they|
Line # 20 [18] |should declare the|
Line # 21 [18] |causes which impel|
Line # 22 [11] |them to the|
Line # 23 [11] |separation.|
Shortest line: 11
Longest line: 20
Not able to do this
There are 11 lines wrapped at 1 characters:
Line # 1 [ 4] |When|
Line # 2 [ 2] |in|
Line # 3 [ 3] |the|
Line # 4 [ 6] |Course|
Line # 5 [ 2] |of|
Line # 6 [ 5] |human|
Line # 7 [ 7] |events,|
Line # 8 [ 2] |it|
Line # 9 [ 7] |becomes|
Line # 10 [ 9] |necessary|
Line # 11 [ 3] |for|
Shortest line: 2
Longest line: 9