Turn a string into an array

This is a discussion on Turn a string into an array within the C++ Programming forums, part of the General Programming Boards category; I'm kind of new to C++ and I want to turn a string into an array. I want to separate ...

Turn a string into an array

I'm kind of new to C++ and I want to turn a string into an array. I want to separate each element of the array by detecting the "+" signs in the string. I know there is a function that can do this, but I just can't remember what it is.

If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Searches one string for tokens, which are separated by delimiters defined in a second string.

strtok considers the string s1 to consist of a sequence of zero or more text tokens, separated by spans of one or more characters from the separator string s2.

The first call to strtok returns a pointer to the first character of the first token in s1 and writes a null character into s1 immediately following the returned token. Subsequent calls with null for the first argument will work through the string s1 in this way, until no tokens remain.

The separator string, s2, can be different from call to call.

Note:

Calls to strtok cannot be nested with a function call that also uses strtok. Doing so will causes an endless loop.

Return Value:

strtok returns a pointer to the token found in s1. A NULL pointer is returned when there are no more tokens.

Ok so you want to call explode() on a string and get back an array of strings right?

So you should want as a return from your explode function a vector<string>
You should pass your explode function a string or string & and a delimiter character.

Inside your explode function you'll have to call find to find the the occurances of the delimiter character. As you find them you simply call substr to pull out the piece you want and then push_back it into the vector. Once you are done parsing the string then you return the vector.