is there any function in c++ which is the same as strtok() function in c,if there is can u give a simple example
This is a discussion on token within the C++ Programming forums, part of the General Programming Boards category; is there any function in c++ which is the same as strtok() function in c,if there is can u give ...
is there any function in c++ which is the same as strtok() function in c,if there is can u give a simple example
strtok() can be used in c++ the same way its used in c
just include <cstring>
If you mean with C style strings then you can just use strtok in <cstring>, if you mean with the string class then you can do something like this
Code:#include <iostream> #include <string> string bad_char = " "; string next_token(string& s, string::size_type& pos) { size_t begin = s.find_first_not_of(bad_char, pos); size_t end = s.find_first_of(bad_char, begin); pos = end; if (begin == string::npos) { return string(); } else if (end == string::npos) { return s.substr(begin); } else { return s.substr (begin, end - begin); } } int main() { string::size_type pos = 0; string s = "Breaking up a string"; string t; while (!(t = next_token(s, pos)).empty()) { cout<< t <<endl; } }
*Cela*