I have strings of following form
a = x + y or abc = xyz + 5 or 6 + 5 or f(p)
What i need is to tokenize the string such that I read each operator and operand
so for a = x + y tokens returns should be a,=,x,+,y and in case of abc=xyz+5 it should return abc,=,xyz,+,5. please note that there may or may not be spaces between operator and operands
this is what I have tried
void tokenize(std::vector<std::string>& tokens, const char* input, const char* delimiters) {
const char* s = input;
const char* e = s;
while (*e != 0) {
e = s;
while (*e != 0 && strchr(delimiters, *e) == 0) {
++e;
}
if ( *e != ' ' && strchr(delimiters, *e) != 0 ){
std::string op = "";
op += *e;
tokens.push_back(op);
}
if (e - s > 0) {
tokens.push_back(std::string(s,e - s));
}
s = e + 1;
}
}
You can use this implementation.
First argument is the std::string you want to tokenize, second argument is the delimiter you want to use. It returns a vector of strings tokenized. Very simple yet efficient.