Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract individual words from string c++

Tags:

c++

string

vector

I am trying to make a C++ program that receives user input, and extracts the individual words in the string, e.g. "Hello to Bob" would get "Hello", "to", "Bob". Eventually, I will be pushing these into a string vector. This is the format I tried to use when designing the code:

//string libraries and all other appropriate libraries have been included above here
string UserInput;
getline(cin,UserInput)
vector<string> words;
string temp=UserInput;
string pushBackVar;//this will eventually be used to pushback words into a vector
for (int i=0;i<UserInput.length();i++)
{
  if(UserInput[i]==32)
  {
    pushBackVar=temp.erase(i,UserInput.length()-i);
    //something like words.pushback(pushBackVar) will go here;
  }  
}

However, this only works for the first space encountered in the string.It does not work if there are any spaces before the word (e.g. if we have "Hello my World", pushBackVar will be "Hello" after the first loop, and then "Hello my" after the second loop, when I want "Hello" and "my".) How do I fix this? Is there any other better way to extract individual words from a string? I hope I haven't confused anyone.

like image 457
VVSTITAN Avatar asked Aug 20 '16 03:08

VVSTITAN


People also ask

How do I extract certain words from a string?

Extract a specific word from a string using find() method. If we want to extract a specific word from the string and we do not know the exact position of the word, we can first find the position of the word using find() method and then we can extract the word using string slicing.

Is there a split function in C?

In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How do I extract a specific word from a string in C++?

using C++ istringstream class. Assign input string to istringstream object and within a loop extract each word . for example, if the string is “hello world” then hello and world will be extracted and it can be stored in a data structure or process it.


2 Answers

See Split a string in C++?

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

using namespace std;

void split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
}


vector<string> split(const string &s, char delim) {
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}

So in your case just do:

words = split(temp,' ');
like image 114
macco Avatar answered Nov 05 '22 18:11

macco


You can use the operator >> direct to a microbuffer (string) to extract the word. (getline is not needed). Take a look at the function below:

vector<string> Extract(const string& Text) {
    vector<string> Words;
    stringstream ss(Text);
    string Buf;

    while (ss >> Buf)
        Words.push_back(Buf);

    return Words;
}
like image 28
TheArchitect Avatar answered Nov 05 '22 20:11

TheArchitect