November 30th, 2007, 06:31 AM
Looping through a string
Im trying to loop through a string to get all layout of words in a forward manner and then search for patterns elsewhere.
I first created this:
for(int j = 0; j<input.length()+1; j++)
if (input[j] == char(32) || j == input.length())
basically pushes integers onto a vector of ints, with the start and end position, and also where any spaces are.
Then i try ot loop as so:
for(vector::iterator ii = spacePos.begin(); ii!=spacePos.end(); ii++)
for(vector::iterator ii2 = spacePos.begin(); ii2!=spacePos.end(); ii2++)
if (*ii2 == 0 || *ii >= *ii2) continue;
if (input.substr(*ii, *ii2) != "")
so if the input line was "hello i am" it would 'hopefuly' do this:
"hello i am"
but the problem is, i'm counting the spaces in the first for loop and storing them and after the first iterator of the for loop for ii, it's going out of sync and the longer the string the more this affect occurs:
"hello i am"
" i am"
i tried trimming but it just causes so many problems with long strings so i really want to sort this out in the first for loop where i obtain all the space positions.
Does anyone have any ideas? or maybe even a better way to do what i want?
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!