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 601347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:42:18+00:00 2026-05-13T16:42:18+00:00

I am working on understanding recursion, and I think I have it down alright…

  • 0

I am working on understanding recursion, and I think I have it down alright… I’m trying to build a search function (like the std::string.find()) that searches a given string for another string for example:

Given (big) string: “ru the running cat”

search (small) string: “run”

I’m trying to return a index for the where the word i am searching for (in the case above it would be **7) the recursive method i have is as follows – i can’t seem to get it to return the index properly.

calling the recursive function:

index = index_of(imput.c_str(), search.c_str())

Recursion method:

int index_of( const char * i, const char * s) {
 int j;
 if (*s == '\0') { return; }
 if (*i == '\0') {
  return NULL;
 }
 else if ( *i == *s ) {
  index_of((i++), (s++));
 }
 else {
  j += index_of((i++), s);
 }
 return j;
}

another foreseeable problem is that when (like in the example – OK it sucks but i need one that worked) it reaches the “ru_” it’s still stuck on the ‘ ‘ (SPACE) -> how would i get it to ‘reset’ the s pointer?

  • 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-05-13T16:42:18+00:00Added an answer on May 13, 2026 at 4:42 pm
    1. Don’t change any state variables. Your code should not include the operator ++ anywhere. You are not trying to loop over a datastructure and change your local variables in some fashion – you are trying to generate an entirely new but smaller problem each time. So, all those ++ operators – whether pre or post increment – are red flags.
    2. You have more than one sub-problem. (…so single function recursion isn’t ideal).

      Let’s look at this systematically.

      Assume you have a working index_of and you just want to call it with input that’s shorter than yours, and that both haystack and needle aren’t empty yet. Then one of two things may be:

      • The haystack starts with the same letter as the needle, and you just need to look a little deeper to verify this.
        – What happens if verification succeeds – what if it fails? is this an index_of subproblem?

      • …or the haystack starts off wrong, and you need to look deeper.
        – Is looking deeper into the haystack an index_of subproblem?

      Notice that if the haystack starts OK it doesn’t necessarily mean that it starts with the full search string – and if it starts OK but does not start with the full search string, you really don’t need to continue looking. That means that the “starts-with” sub-problem is fundamentally different than the index-of sub-problem:

      • index_of: here, failure to find the search string at index 0 means you need to look further.
      • starts_with: here, failure to find the search string at index 0 means you need to stop.

      It is possible to say startswith(haystack, needle):= 0==index_of(haystack, needle), but obviously index_of is a more complicated problem with a more expensive solution – so you shouldn’t do that; it’s also more confusing at first.

    3. Identify your base cases – When either needle or haystack are empty, what does that mean?

    4. Good names help clarify things, and recursion is hard to read at the best of times – Yacoby’s reply for instance has some meaningful choices here.

    Summary

    I’m not going to solve your own puzzle for you, but a few tips in recap…

    • Avoid changing your local variables: you’re trying to define a subproblem and then just call the right function for those newer, shorter parameters. Don’t use side-effects, they make things terribly complex.
    • Recursion doesn’t necessarily mean just one function A calling A – it can be any cyclic call graph that eventually terminates; you may use more functions
    • If needle and haystack start with the same letter, that doesn’t mean that the entire needle is at the start of the haystack – and if it is not, you still need to continue searching
    • This line is wrong: if (*s == '\0') { return 1; }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this working great, but I'd like a deeper understanding of what is
I have recently been working to better my understanding of sorting algorithms and their
In Mongo my understanding is that you can have databases and collections. I'm working
I'm working on some introductory recursion problems and I have a clarifying question I'd
I have a problem understanding why a certain implicit conversion is not working as
I am working on understanding collision detection and have decided to create a program
I'm working on better understanding the application of a depth-first search algorithm. I understand
I'm new to programming and have had a hard time understanding recursion. There's a
I have a recursive function emit : Map<string,LocalBuilder> -> exp -> unit where il
I'm working at understanding best practices for MVC (using CakePHP) and am trying to

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.