2

Possible Duplicate:
Splitting a string in C++

I can't use boost (as I've seen as a solution for a lot of tokenizing questions). First I place a typed command into a string. Command example:

add (name, phone-number)

int main()
{
    string line;
    cin >> line;
    cout << "Reservations>>";
    if(line[0] == 'a'){
    }
}

I need to make sure everything is correct syntactically (that they used parenthesis and commas), which I was going to do by first breaking the string down into strings themselves, placed in a vector. So my first question is: How can I just break off each part separated by a space so that I can push it into the vector? I was then going to compare '(' with the string of vector[1]'s first char ([0]), and ',' with the string of vector[1]'s [line.length()] - how would I go about referencing certain characters in the string located in a vector?

Community
  • 1
  • 1
Will Nasby
  • 1,068
  • 2
  • 16
  • 39
  • This is a duplicate of [this](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c?rq=1) and [this](http://stackoverflow.com/questions/236129/splitting-a-string-in-c?lq=1) and the many others on SO. – Rapptz Jan 28 '13 at 07:28
  • Thanks for the links, but do you know how to reference a specific character in the string located in a vector? – Will Nasby Jan 28 '13 at 07:35
  • Are you just checking brackets balance? – Alex Chamberlain Jan 28 '13 at 08:37

1 Answers1

1

You can try using strtok().You can't use strtok directly on a C++ std::string. It requires a mutable zero-terminated C-style string, and there is no standard way to access the contents of a std::string in that form.

For more on this, go through this

Community
  • 1
  • 1
sr01853
  • 6,043
  • 1
  • 19
  • 39