-1

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;
}
Eliana Lopez
  • 161
  • 1
  • 1
  • 8
  • 2
    Does this answer your question? [How do I iterate over the words of a string?](https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string) – kesarling He-Him Aug 26 '21 at 03:25
  • 2
    *"I am having trouble [with a goal]"* *"I believe my main issue has to do with [something]"* -- while you did set the context for your issue, you failed to describe what your issue is. Without that detail, I don't see this question being useful to future visitors. – JaMiT Aug 26 '21 at 03:29
  • True. It is usually recommended to post, in the least, a screenshot of the output as well as the expected output – kesarling He-Him Aug 26 '21 at 03:31
  • add a `word = s.substr(first, last); words.push_back(word);` outside for-loop, since there is no space after the last word and hence the else part will not be executed. – susanth29 Aug 26 '21 at 03:36
  • 1
    @kesarlingHe-Him I think to paste the text is better. – prehistoricpenguin Aug 26 '21 at 03:36
  • @prehistoricpenguin, correct. That is why I said *in the least* :) – kesarling He-Him Aug 26 '21 at 03:36
  • @kesarlingHe-Him Sorry, "in the least" is not strong enough. Take a look at [ask] and note how that puts "**DO NOT post images of code, data, error messages, etc.**" in boldface. It is NOT recommended to post a screenshot of the output. It is recommended to post the output, and the output is to be in the form of text if that is possible (which should be the case here). – JaMiT Aug 26 '21 at 03:58

3 Answers3

3

I suggest:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>

using namespace std;

vector<string> wordChopper(const string & s)
{
    istringstream iss(s);
    return vector<string>(istream_iterator<string>{iss}, istream_iterator<string>());
}

int main()
{
    for (auto & iter : wordChopper("hello there how are you"))
    {
        cout << iter << endl;
    }

    return 0;
}
secuman
  • 539
  • 4
  • 12
2

I have given the widely used method in the comment. However, if you want to write your own function, I would suggest trying out something on the lines of:

std::vector<std::string> stringTokeniser(std::string originalString, char delimiter = ' ') {
    std::vector<std::string> tokensVector;
    std::string word;
    for (const auto character : originalString) {
        if (character != delimiter) {
            word.push_back(character);
        }
        else {
            tokensVector.push_back(word);
            word.clear();
        }
    }
    tokensVector.push_back(word);
    return tokensVector;
}
kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
2

I am having trouble splitting words of a string into a vector

There is no need for indexing, start and first positions, etc. if the words are separated by spaces.

Usage of std::stringstream accomplishes this:

#include <vector>
#include <string>
#include <sstream>
#include <iostream>    

std::vector <std::string> wordChopper(std::string s)
{
    std::vector<std::string> words;
    std::stringstream strm(s);
    std::string word;

    while (strm >> word)     
       words.push_back(word);
    return words;
}

 int main()
 {
    auto v = wordChopper("hello there how are you");
    for (auto s : v)
      std::cout << s << "\n";
 }

Output:

hello
there
how
are
you
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45