How to reversely strtok a C++ string from tail to head?

c++, strtok

Solution

You could do this with `strpbrk`:

char string[] = "hello+stack_over-flow";

char *pos = string;
while (*pos != '\0' && (pos = strpbrk(pos, "+-_")) != NULL)
{
    /* Do something with `pos` */

    pos++;  /* To skip over the found character */
}

Problem

I think I need a reverse version of strtok, like: ``` char* p = rstrtok(str, delimeters); ``` For example, sequentially get the position of `'-'`, `'_'` and `'+'` in the string "hello+stack_over-flow" using a delimeter set of "+_-" I only care about the delimeters, and their position, (not the content between), so I guess the `boost::split_iterator` is not appropriate here. Is there any existing utility function I can leverage? or any solution to deal with this kind of situation? Furthermore, since I am doing C++, is there any convenient approach to avoid this old fashion C? (I searched "reverse strtok" but merely get "stack over flow" to "flow over stack" stuff...)

Original source