Firstly, use operator!=() on iterators, not operator<():
while (it != sentence.end())
Secondly, this is backwards: while (*it != ' ' && it != sentence.end())
You do something with the iterator, than check if the iterator is valid. Rather, you should check if it's valid first:
while (it != sentence.end() && *it != ' ')
Thirdly, you should use ++iterator over iterator++, though this isn't related to your crashing.
Fourth, a main issue is here:
*it = '\n';
Because of the preceeding check, while (it != sentence.end()
, it's possible to reach that iterator dereference while being at the end. A fix would be to do this:
if (it != sentence.end() && nextWordLength > distanceFromWidth)
So now if you have reached the end, you stop.
After fixing the previous issue, now the only problem is this:
//skip the space
++it;
This assumes that the character you are skipping is in fact a space. But what about the end of the string? Run this function with this string:
"a test string " // <- space at end
And it will succeed; it skips the space, putting the iterator at end()
, the loop exits and success.
However, without the space it will crash, because you have reached the end, and are skipping past the end. To fix, add a check:
//skip the space
if (it != sentence.end())
{
++it;
}
Resulting in this final code:
std::string wordWrap(std::string sentence, int width)
{
std::string::iterator it = sentence.begin();
//remember how long next word is
int nextWordLength = 0;
int distanceFromWidth = width;
while (it != sentence.end())
{
while (it != sentence.end() && *it != ' ')
{
nextWordLength++;
distanceFromWidth--;
++it;
}
if (it != sentence.end() && nextWordLength > distanceFromWidth)
{
*it = '\n';
distanceFromWidth = width;
nextWordLength = 0;
}
//skip the space
if (it != sentence.end())
{
++it;
}
}
return sentence;
}
You might notice this seems like it has a lot of redundant checks. This can be fixed:
std::string wordWrap(std::string sentence, int width)
{
std::string::iterator it = sentence.begin();
//remember how long next word is
int nextWordLength = 0;
int distanceFromWidth = width;
while (it != sentence.end())
{
while (*it != ' ')
{
nextWordLength++;
distanceFromWidth--;
++it;
// check if done
if (it == sentence.end())
{
return sentence;
}
}
if (nextWordLength > distanceFromWidth)
{
*it = '\n';
distanceFromWidth = width;
nextWordLength = 0;
}
//skip the space
++it;
}
return sentence;
}
Hopefully that helps!