I was wondering. Is there any function like strtok to split into tokens a string, which is c++ standard? Also is there a way to store those tokens into separate char arrays?

Thank You

std::string's find() method then use std::string's substr() method to extract the string. IMO its better than strtok() because it doesn't modify the original string.

If you were using c++/CLR then it's String class has a Split() method, but that isn't standard c++ and works only with Microsoft compilers.

Thanks for the response Dragon, I will look into it. As for saving the tokens into different char arrays, do you know how? Edit: Oh this done using substr right?

Oh and just for curiosity, in what manner does strtok modifies the string?

just put the strings in a vector: vector<string> ay; >>Oh and just for curiosity, in what manner does strtok modifies the string?
It replaces the separator token (such as a space or a comma) with '\0' then returns a pointer to the beginning of that segment of the character array. Therefore you can not pass it a string literal.

Thanks Dragon. Even though I don't know anything about vectors I would look into it. But maybe you can help me. I searched the find() and substr() function and I wasn't able to split the string. Maybe you can show an example....sorry fr bothering you again.

A simple example

#include <iostream>
#include <vector>
#include <string>

using namespace std;
int main()
{
std::vector< std::string > theList;
std::string str = "Hello World";
size_t pos = str.find(' ');
theList.push_back( str.substr(0, pos) );
theList.push_back(str.substr(pos+1));

// now display the two string
for(size_t i = 0; i < theList.size(); i++)
    cout << theList[i] << "\n";
}

Thanks Drag! Kind of advance for me but I would study it anyway.

If you're up to getting to grips with Qt, the QString class has a comprehensive range of extra methods that don't feature in std::string . One of these is split() . The advantage of this method is that Qt's split() function accepts all kinds of things, including regular expressions. It's a lot of baggage if you only want something simple, but it's all there if you want a very complete way of doing it, it's there!

There is also a Boost library called string algorithms that also has a split function.

Hope that helps :)

Well here is the program I wanted to implement the spliting of the string.

So here is how it should work: It should accept a string in which there is a full operation (ex. 23 + 34) it should split the string into three parts. The first and the third part should be the numbers and the second should be the operation sign. Then it should convert the first and third string into a double number (using atof). Then use the if statement and depending on the sign it should add, multiply, substract or divide the numbers.

There are a couple of errors (too long to copy, but most of them repeat). So please compile it.

Probably there are some stupid errors because I am a begginer... sorry.

WAIT: I found some erros. I will fix them and then if had another problem, I will post it
Thanks

Oh and ravenous thanks for your suggestion but kinda looks to complicated for me. Thanks anyway.

Well I Dragon I would like to thank you for all the help you gave me. It really helped me a lot!.

The solution you gave me was perfect.

Thanks Again,

AssaultM16

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.