Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:15:33+00:00 2026-06-11T02:15:33+00:00

Consider this function I wrote real quick to find the last occurrence of a

  • 0

Consider this function I wrote real quick to find the last occurrence of a given char in a string and return it’s position within the array of chars that physically is the string:

size_t strlstchar(const char *str, const char ch)
{
    char *chptr = strrchr(str, ch);
    return chptr - str;
}

I just typed this up real quick here (haven’t compiled or anyting yet) just because I have questions about a few things.

To me this seems like the simplest solution to find which array element holds the last instance of a particular char, but I have no idea how it works. I just made this following the documentation of strrchr, so it’s technically strrchr doing all the work. I just can’t imagine this being the best way (in terms of performance) to achieve this, and was hoping somebody could give some input on what would be the best way to do this.

Is strrchr an efficient way to do this? Or is strrchr best left for some other use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:15:34+00:00Added an answer on June 11, 2026 at 2:15 am

    The approach you used is perfectly fine – unfortunately, array operations are expensive. Strrchr in most implementations simply steps through the string beginning from its end until it finds a matching character. That’s O(n) time. Then you perform a subtraction which is O(1). This is not that bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
Consider: function Shape() { this.name = "Generic"; this.draw = function() { return "Drawing "
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Consider this code: var img = new Image(); img.onload = function() { console.log(this.width); };
Before Consider to have a class and a global function: This is, for example,
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider this code: enum { ERR_START, ERR_CANNOTOPENFILE, ERR_CANNOTCONNECT, ERR_CANNOTCONNECTWITH, ERR_CANNOTGETHOSTNAME, ERR_CANNOTSEND, }; char* ERR_MESSAGE[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.