This is sort of a complicated question so I'll keep it as brief and simple as I can.
I found a function that tokenizes a string based on spaces. This works fine.
However, the string containes newlines and so any token that has a newline after it is stored as the token with the newline.
I built a second loop after the first tokenizing loop that iterates through the vector, finds a string with a newline and inserts a blank string. I've fiddled with the position of the increment/decrement operators with no success -- the 'blank' string is indeed inserted but never in the correct position (either before the string containing the newline or after the following string). Part of this stems from having avoided string manipulations like the plague so now I'm stuck. Obviously what I wrote is not what I need and isn't correct.
Code:
vector<string> mTokens;
// Skip leading spaces and find position of the first word.
Uint lastPosition = text.find_first_not_of(" ", 0);
Uint curPosition = text.find_first_of(" ", lastPosition);
// Iterate over string and add individual words to a list of words.
while(curPosition != string::npos || lastPosition != string::npos)
{
mTokens.push_back(text.substr(lastPosition, curPosition - lastPosition));
lastPosition = text.find_first_not_of(" ", curPosition);
curPosition = text.find_first_of(" ", lastPosition);
}
vector<string>::iterator i = mTokens.begin();
// Iterate through all words to find newline characters and add a blank line where newlines are found.
while(i != mTokens.end())
{
if(i->find("\n") != string::npos)
i = mTokens.insert(++i, "");
i++;
}
Secondly, there may be cases where there are several newlines. How can I properly account for that? I figured that, assuming a particular string has more than one newline after it, that I could simply truncate that newline off the string but this has the effect of truncating characters off the string, not stripping the newline.
Thanks for your time.