-1

I am trying to split the string into three parts and print each part backwards. So when the user enters "HelloWorld" it should print "dlroWolleH". I don't know where i am wrong, it flips like pair.

import java.util.*;
public class recursion
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner (System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();
        System.out.print(revRec3(str));
    }

    public static String revRec3(String str)
    {
        if ((str.length() == 1)|| (str.length()==0 || str.length()==2))
        {
            return str;
        }

        String left = str.substring(0, (str.length() / 3)),
        middle = str.substring((str.length()/3) ,(str.length()-left.length())),
        right = str.substring((str.length()-middle.length()),str.length());
        String revLeft = revRec3(left);
        String revMiddle = revRec3(middle);
        String revRight = revRec3(right);

        return revRight + revMiddle + revLeft;
    }        
}
doey
  • 1
  • 2
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Sep 26 '17 at 03:08
  • Print the result of your split, and you'll find your error: `HelloWorld` is split into `Hel`, `loWo`, `orld`. Oops!! The `o` in `World` is included twice. – Andreas Sep 26 '17 at 03:10
  • Also, if input is 2 characters long *(and it will be on one of the recursions)*, you return it as-is, without reversing it. Oops!!! – Andreas Sep 26 '17 at 03:13

4 Answers4

0

Maybe you are trying to achieve this

import java.util.Scanner;

public class Main {

  public static void main(String args[])
  {
    Scanner sc = new Scanner (System.in);
    System.out.print("Enter a string: ");
    String str = sc.next();
    System.out.print(revRec3(str));
  }



public static String revRec3(String str)
  {
    if ((str.length() == 1)|| (str.length()==0 || str.length()==2))
    {
  return str;
}

String left = str.substring(0, (str.length() / 3)),
    middle = str.substring((str.length()/3) ,(str.length()-left.length())),
    right = str.substring((str.length()-middle.length()+1),str.length());
String revLeft = revRec(left);
String revMiddle = revRec(middle);
String revRight = revRec(right);
return revRight + revMiddle + revLeft;


 }



private static String revRec(String middle) {
    StringBuilder sb = new StringBuilder(middle);
   return sb.reverse().toString();
  }
}
Kamesh Joshi
  • 382
  • 2
  • 10
  • 2
    The question is not about how to reverse a string. It's an *assignment* for testing the programmers proficiency in recursive algorithms, and likely explicitly states exactly what question says: It must split input into 3 parts and flip the parts, and each part must then be reversed using recursion. Down-vote from me, as the answer is not useful to the question. – Andreas Sep 26 '17 at 03:05
  • Why on earth are you adding the string one character at a time, when `new StringBuilder(s).reverse().toString()` will do it? – Andreas Sep 26 '17 at 03:06
  • check the updated code, I think this will solve your problem – Kamesh Joshi Sep 26 '17 at 03:21
  • There is still no *recursive* call in that code. Seems you don't understand the scope of the question, i.e. what recursion is and how to use it. Good thing you didn't get that assignment, as you'd have failed it. – Andreas Sep 26 '17 at 03:23
0

You have a base case that is returning the string unchanged:

str.length()==2

In that case you need to reverse the string before returning it.

Daniel
  • 21,933
  • 14
  • 72
  • 101
0

I believe this is what you were going for.

public static void main(String args[])
{
        Scanner scanner = new Scanner (System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();
        System.out.print(threeSplitString(str));
}

public static String threeSplitString(String str)
{
    if (str.length() < 3)
    {
        return str;
    }
    int len = str.length();

    String p1 = str.substring(len * 0 / 3, len * 1 / 3); // First Third
    String p2 = str.substring(len * 1 / 3, len * 2 / 3); // Second Third
    String p3 = str.substring(len * 2 / 3, len * 3 / 3); // Third Third

    // The recursion
    p1 = threeSplitString(p1);
    p2 = threeSplitString(p2);
    p3 = threeSplitString(p3);

    return p3 + p2 + p1;
}
Hashim Kayani
  • 259
  • 1
  • 2
  • 12
0

Here my javascript code, hope it's helpful.

function rev(input) {
  let len = input.length;
  if(len < 2) return input;
  if(len == 2) return input[1]+input[0];
  let left = input.substring(0, Math.round(len/3))
  let mid = input.substring(Math.round(len/3), Math.round(len-len/3))
  let right = input.substring(Math.round(len-len/3), len)

  return rev(right) + rev(mid) + rev(left);
}

But I don't know why do we need to slip split into 3 parts, we already do with 2 parts

function rev(input) {
  let len = input.length;
  if(len < 2) return input;
  if(len == 2) return input[1]+input[0];
  let left = input.substring(0, Math.round(len/2));
  let right = input.substring(Math.round(len/2), len);

  return rev(right) + rev(left);
}
Quan Vo
  • 1,639
  • 2
  • 13
  • 28