I am having trouble splitting words of a string into a vector. I am trying to keep track of each word with a first and last integer. I believe my main issue has to do with how I am iterating over the string.
What would be ways to improve this function?
Input: "hello there how are you"
Actual Output: "hello", "there", "how", "are"
Expected Output: "hello", "there", "how", "are", "you"
std::vector <std::string> wordChopper(std::string& s)
{
std::vector<std::string> words;
int first = 0;
int last;
std::string word;
for(unsigned int i = 0; i < s.size(); i++)
{
if(s[i] != ' ')
{
last++;
}
else
{
word = s.substr(first,last);
words.push_back(word);
first = i+1;
last = 0;
}
}
return words;
}