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

3

6

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...)

Tuberculosis answered 18/6, 2012 at 6:35 Comment(1)
I don't think there is a function that will do this for you. I think you should use a loop.Fleda
V
3

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 */
}
Vagus answered 18/6, 2012 at 6:42 Comment(2)
thanks @jo, i think i like strpbrk, but is there a reverse version of it? or i`ll reverse the string first and use this. Hope will pass the code review ...Tuberculosis
@Tuberculosis As far as I know, there is no reverse version of it, so unfortunately you have to reverse the string first.Vagus
D
8

You could roll your own using strrchr.

If you use C++ style std::string you can leverage string::find_last_of.

Dionisio answered 18/6, 2012 at 6:40 Comment(4)
thanks @dirk, string::find_last_of is good, but i have to iterate the collection of delimeters every time; and compare the distances to see who is the nearest to tail.Tuberculosis
@Lyn: This was not specified in your question. Can you edit your question and post what exactly you would like as your output (possibly with some examples)?Dionisio
I think I have to defend myself... the example and output is "For example, sequentially get the position of '-', '' and '+' in the string "hello+stack_over-flow" using a delimeter set of "+-""; Also, i guess we all do want to do things in a burdensome and error prone way.Tuberculosis
@Lyn: The part that *you have to compare the distances ... * wasn't there. This is not about defending. It's about optimizing your time and that of people who are trying to help you. FWIW, this question might well have been closed since you don't tell us what you have tried.Dionisio
V
3

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 */
}
Vagus answered 18/6, 2012 at 6:42 Comment(2)
thanks @jo, i think i like strpbrk, but is there a reverse version of it? or i`ll reverse the string first and use this. Hope will pass the code review ...Tuberculosis
@Tuberculosis As far as I know, there is no reverse version of it, so unfortunately you have to reverse the string first.Vagus
G
0

strtok is pretty simple in the most basic versions - a couple hundred lines at most. If you google "strtok filetype:c" you can see how it's implemented in the various libraries.

A very naive solution would be to reverses the string first, and then do the strtok(). This is bad for long strings though, but if you need performance, go roll your own strtok().

Something like this:

void reverse(char* dest, const char* src)
{
    int len = strlen(src);
    int i;
    for(i = 0; i < len; ++i)
        dest[len-i-1] = src[i];
}

EDIT:

Coincidentally I have this Dr Dobbs page open in a tab from a google search yesterday: http://www.drdobbs.com/conversations-al-go-rithms/184403801

Godson answered 4/7, 2012 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.