String to characters

I'm currently trying to turn a string into a list of characters.
I am using the stringstream method to do this.

However, I need to know how many characters have been counted before a specific character.

For example;
My string input is: ABCD+DCBA

I am trying to get ABCD split up into characters and store them in 1 array/vector, then determine the operator (+) and store the rest of the string into another array/vector. I need to know how many characters were stored in each array, so 4 characters have been stored in array 1, 4 in the other.

Does anyone know what I should do?
If you are using the C++ string class you don't even need to use a stringstream. You can literally access the C++ strings as if they were an array. In addition to that you can all the various string methods.

I mean, if you are trying to build your own string class than you could just go all oldschool and code the whole thing in C using nothing but char arrays and user kept indexes.

If you want to take advantage of C++ libraries you could do a string.find() on the char '+' and then take a substring of everything before it, put in an array and take another substring of everything after it, and put it in another array.

You need to include the <string> library to use them though.
Last edited on
Must you use arrays or vectors? couldn't you use a basic string for things.

If I am starting from a stringstream someStream; , I guess I would loop until end of file getting a character from the stream. Like char someChar; someStream.get(someChar);

From there I can break things out to any form I need. I hope that gets you starting point.
Ahh, I just forgot you could do that instead of stringstream.
Question, how do I iterate through a string to find the + and store everything before it into an array? I've never used this before.
I would start with the reference on string of the c++ reference on this site.
http://www.cplusplus.com/reference/string/string/

it will be a good starting point. There are a number of ways to iterate through a string.
Yea, read up on that stuff. C++ strings are very powerful and if you ever try to code in plain C you will miss them dearly.

You can start by traversing through them in a for loop using string.size() as an ending point.

Then once you get comfortable you can use the substring method and you won't even need loops in your case. You just need to use the indexes of the string.find() method and the string.size() method to stay in range.

string[0] will always give you the first char but if it is a null string it will represent a NULL char ('\0'), obviously.
Topic archived. No new replies allowed.