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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:35:19+00:00 2026-06-07T13:35:19+00:00

I am attempting to use recursion to reverse a string but the special twist

  • 0

I am attempting to use recursion to reverse a string but the special twist is that I must start at a specified index return that character contained at the index and then reverse the remaining string after that index and combine the two into one output. I have tried to do this and sometimes it gives an array out of bounds exception and sometimes ends up with more letters than it should have such as this test of helloooo coming out as lloooooo. Any help with tweaking this code would be appreciated as I became really lost when the need for the second parameter was known to me.

  public class ReverseTest
    {
        public static void main(String[] argv) 
        {
            System.out.println(reverse("a", 0));
            System.out.println(reverse("hi", 0));
            System.out.println(reverse("helloooo", 2));
        }


        public final static String reverse(String s, int ind)
        {
            int length = s.length(); //get string length

            if (length <= 1) //if length is 1 or lower just repeat the string nothing to do
                return s; //return string
            else //if we have something to work with...
                return s.substring(ind) + reverse(s.substring(ind + 1, length - 1), ind); //return substring of necessary index + reverse the rest and display in one string
        }
    }
  • 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-07T13:35:21+00:00Added an answer on June 7, 2026 at 1:35 pm

    Not sure what is wrong with your version, but I built my own according to what I find to be an intuitive implementation:

    private static String reversePartially(String initial, int target) {
        return reversePartially(initial, target, target);
    }
    
    private static String reversePartially(String initial, int index, int target) {
        // if we are at the target index put the character at the front
        if(index == target) return initial.charAt(target) + reversePartially(initial, index + 1, target);
        // if we are in the second part return the characters reversed
        if(index > target) return reversePartially(initial, index + 1, target) + initial.charAt(index);
        // base condition
        if(index == initial.length()) return "";
        return "";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In attempting to use std::select1st from <functional> in a VS2008 project I found that
I am attempting to use the data sources pluigin for rubymine 4.0.3 but I
I am attempting to write a interpreter that will turn a string like: vector(random(0,
Attempting to use asp.net mvc's Action Result of File. So it would seem that
I'm attempting to use the rest-client gem to post something, but for some reason,
Im attempting to use the Adjusted Rand Index to compare clustering results. Here, I
I am attempting to use the Exhibit pattern in a padrino application but to
I'm attempting to use the sample broker that comes with ActiveMQ 5 with an
I am attempting to use Cloud9 IDE for the first time, and start a
Attempting to use the data series from this example no longer passes the JSONLint

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.