I'm making an indexOfReverse utility function in my C++ program, and I have caught a bit of a snag. Implementing the offset and maxOffset in my indexOf was very intuitive to me.. These start from the beginning of the string (+offset), and go forth to the end of the string (or maxOffset if given).

While implementing offsets for the indexOfReverse, though, I was wondering what the most logical way to do this is? Should the offset be from the beginning of the string, or end of the string? Is there an established norm for this I am missing somewhere? I tried explaining it to my duck, but he just looked at me funny. I'm leaning towards thinking of it from the beginning, because of its ease-of-use being more valuable to me than its counter-intuitiveness.. considering the offset from the end just makes things harder to think about in my head.

I have looked at how it's done in a string library for C++, PHP, and Python, and what seems common in the description of each is finding the last of occurrence of a string where the offset is considered from the beginning, rather than doing true reverse-string searching... But I desire true reverse-string searching rather than last occurrence ofs. Is there a most logical way to consider this, though, so I can move on?


Take a look at how it's done in the C++ standard library: string::rfind. Better yet, just use that function instead of writing your own.

They take offsets from the beginning of the string. Doing so makes it easier to use the result in, say, substr:

string str         = "I haven't done C++ in a while.";
size_t last_in     = str.rfind("in");
string last_phrase = str.substr(last_in); // "in a while."

There's actually two decisions to make for an rfind function:

  • Should the result index be based on the beginning of the haystack?

    I haven't done C++ in a while.

    or the end of it?

    I haven't done C++ in a while.
  • Should the result index be the beginning of the found result?

    I haven't done C++ in a while.

    or the end of it?

    I haven't done C++ in a while.

If we answer "end" to either of these questions, we have to answer one more question: does "end" mean the last character?


or the character after it?


The latter is more consistent with the regime of beginning-based indexes.

| improve this answer | |
  • Same as rwong, good point about re-using results between these related string manipulation functions. Given that, I think consistency here is most important, because the less we have to worry about thinking (converting/understanding results), the easier it will be to move on and have time for more fun puzzles :) I would use an est'd library if I wasn't trying to actively get my hands dirty and get into the nitty-gritty of this fine language.. eventually I will jump into them, but I want understand my own misunderstandings before I start trying to debug others', haha. Thanks for this help! – abelito Jul 22 '12 at 6:30

For most string users, measuring the offset from the beginning of the string regardless of the method called would make the most sense.

Recognize that when a user wants to get the last occurrence of something, it does not imply that the user have a logical use of the "reversed string". If it were so, the user could have simply done a reverse first, and proceed with whatever operation desired. Rather, the user is more likely interested in splitting the string into three or more parts:

Mr. Thomas A. Anderson

Namely, everything before, the matched substring, and everything after.

Because of this, there is a need to pass the string offsets obtained from one method into another method, between find and rfind. Having two definitions just complicates the matter.

A more nuanced approach would be to define an enumerator of matches, a.k.a. Regex. This enumerator allows navigating any number of matches. For each occurrence, the positions of the first character and last character can be queried.

| improve this answer | |
  • You make a good point about consistency between functions, as I may be wanting to pass the output from one into another right away.. If there was not consistency there, I would be forced to do a lot of blue-smokery and conversions that would eventually frustrate me. I didn't mention it in my question because it wasn't yet relevant, but I will be using this crude function to search relatively large text files, and speed is a must, or at least my best attempt at it in the simplest way. – abelito Jul 22 '12 at 6:12
  • 1
    @abelito: One advice is to use negative values for positions that count from backwards, like in Python. – rwong Jul 22 '12 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.