Nested strtok function problem in C [duplicate]

You cannot do that with strtok(); use strtok_r() from POSIX or strtok_s() from Microsoft if they are available, or rethink your design. char *strtok_r(char *restrict s, const char *restrict sep, char **restrict lasts); char *strtok_s(char *strToken, const char *strDelimit, char **context); These two functions are interchangeable. Note that a variant strtok_s() is specified in an … Read more

Split a string into words by multiple delimiters [duplicate]

Assuming one of the delimiters is newline, the following reads the line and further splits it by the delimiters. For this example I’ve chosen the delimiters space, apostrophe, and semi-colon. std::stringstream stringStream(inputString); std::string line; while(std::getline(stringStream, line)) { std::size_t prev = 0, pos; while ((pos = line.find_first_of(” ‘;”, prev)) != std::string::npos) { if (pos > prev) … Read more

Parse (split) a string in C++ using string delimiter (standard C++)

You can use the std::string::find() function to find the position of your string delimiter, then use std::string::substr() to get a token. Example: std::string s = “scott>=tiger”; std::string delimiter = “>=”; std::string token = s.substr(0, s.find(delimiter)); // token is “scott” The find(const string& str, size_t pos = 0) function returns the position of the first occurrence … Read more