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

  • Home
  • SEARCH
  • 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 7635273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:23:37+00:00 2026-05-31T07:23:37+00:00

I have a recursive function to find the starting index of a substring within

  • 0

I have a recursive function to find the starting index of a substring within a string. I am learning to use recursion, so the find function is not allowed. I believe I have met most of the conditions. This function is supposed to find the correct index in the string. If it is blank it returns -1.

Here is the real problem. If I enter a string “nothing” and search for “jax” it doesn’t return -1. I don’t understand why. Any help please? Here is the code:

The user would enter string s and t passed into below:

int index_of(string s, string t)
{
    int start = 0;
    int len2 = t.length();
    int index = 0;

    if (s == "")
    {
        return -1;
    }
    else if (s.substr(1).length() <= t.length())
    {
        return -1;
    }
    else if ( s.substr(start, len2) == t)
    {
        return index;
    }
    else
    {
        index ++;
        return index + index_of(s.substr(1), t);
    }
    return -1;
}
  • 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-31T07:23:38+00:00Added an answer on May 31, 2026 at 7:23 am

    There are several problems — some minor ones, and some quite important ones.

    1. You have two variables, start and index, to indicate “the current position”, but one would be enough.

    2. index can only be 0 or 1. Therefore, the way it is currently written, you could easily get rid of index and start altogether.

    3. Important: When, during the final recursion, the end of the string is reached, you return -1 to the previous recursive call. Then, because of the way the recursive calls are done, you add 1 and return that to the previous call, and so forth. The value finally returned is the -1 plus the length of the string. That is why you get strange results.

    4. This comparison

      if (s.substr(1).length() <= t.length())
      

      does not make much sense.

    Taking all of this into account, here is an improved version:

    #include <iostream>
    #include <string>
    
    int index_of(
      const std::string &s,
      const std::string &t,
      const size_t index)
    {
      int len2  = t.length();
    
      if ((s.length() - index) < t.length())
        return -1;
      else if (s.substr(index,len2) == t)
        return index;
      else
        return index_of(s,t,index + 1);
      return -1;
    }
    
    /** Overloading, so you can call index_of with just
        two arguments */
    int index_of(const std::string &s, const std::string &t)
    {
      return index_of(s,t,0);
    }
    
    /** Some test cases. */
    int main()
    {
      std::cout << index_of("hello","ello") << std::endl;
      std::cout << index_of("nothing","jax") << std::endl;
      std::cout << index_of("hello","llo") << std::endl;
      std::cout << index_of("hello","lo") << std::endl;
      std::cout << index_of("hello","o") << std::endl;
      std::cout << index_of("hello","hel") << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some sort of recursive function, but I need to parse a string,
I have a recursive jQuery function that has something like this: parentItem.find('> div:first-child >
I have a recursive function reading a table of contents of documents from a
I have a simple recursive function RCompare() that calls a more complex function Compare()
Is it possible to have a PHP function that is both recursive and anonymous?
I have a recursion function that parses an object/array with a global variable. If
I have a recursive algorithm which steps through a string, character by character, and
I have a simple recursive function to write in VBA that does the following
Is there a recursive find function for a find in emacs? I thought the
I have the following recursive function which works... up until a point. Then the

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.