Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*In Java Given the main method below, write a recursive method called “recursion

ID: 3906104 • Letter: #

Question

*In Java

Given the main method below, write a recursive method called “recursion1” which accepts three strings as arguments. You may let the class name be “Q1”. Let the first parameter of recursion1 be called s1, the second s2, and the third s3. The method will find the first instance of s3 in s1, and take the substring from up to that position of s3 in s1, and append that substring to the output. It will then do the same thing with s2. It will pass to the next call of the recursion the entire substring from after s3 was found to the end of the string for s1, and then s2. The third argument passed to the recursion will be s3, unmodified.

You may only use indexOf, length, and substring to work with the strings in the method. You must not use any loops, it must be recursive.

So, if I have “and this or that” as s1 with “th” for s3:

-We have “and_this_or_that”

-Where what is in blue (underscores added for clarity, they represent space) is appended to the output and green is passed to the next recursive step. What is highlighted in red is automatically removed because of the use of substring.

For instance, if I have “this or that”, “they’re here and they’re there”, and “th” passed to the method:


This is how the recursion will behave:

s1                                s2                                                        s3            output

“this or that”               “they’re here and they’re there”            “th”     (original call)

“is or that”                   “ey’re here and they’re there”             “th”     “”

“at”                              “ey’re there”                                       “th”     “is or ey’re here and ”

“”                                 “ere”                                                    “th”     “is or ey’re here and at ey’re ”

“”                                 “”                                                         “th”     “is or ey’re here and atey’re ere”

Sample Output:

Please input the first string: this or that

Please input the second string: they're here and they're there

Please input the substring to find: th

is or ey're here and atey're ere

Another Sample:

Please input the first string: hello there help me out

Please input the second string: why hey how are ya

Please input the substring to find: he

why llo ty how are yare lp me out

Main Method:

      public static void main(String args[]){

            Scanner sc = new Scanner(System.in);

            System.out.print("Please input the first string: ");

            String s1 = sc.nextLine();

            System.out.print("Please input the second string: ");

            String s2 = sc.nextLine();

            System.out.print("Please input the substring to find: ");

            String s3 = sc.nextLine();

           

            System.out.println(recursion1(s1, s2, s3));

           

            sc.close();

      }

Explanation / Answer

Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

Q1.java
======


import java.util.Scanner;

public class Q1 {
public static String recursion1(String s1, String s2, String s3)
{
if(s1.equals("") && s2.equals(""))
return "";

String subS1 = "";
String subS2 = "";
int index1 = s1.indexOf(s3);
int index2 = s2.indexOf(s3);

if(index1 != -1)
{
subS1 = s1.substring(0, index1);
s1 = s1.substring(index1 + s3.length());
}
else
{
subS1 = s1;
s1 = "";
}

if(index2 != -1)
{
subS2 = s2.substring(0, index2);
s2 = s2.substring(index2 + s3.length());
}
else
{
subS2 = s2;
s2 = "";
}
return subS1 + subS2 + recursion1(s1, s2, s3) ;
}
  
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Please input the first string: ");
String s1 = sc.nextLine();
System.out.print("Please input the second string: ");
String s2 = sc.nextLine();
System.out.print("Please input the substring to find: ");
String s3 = sc.nextLine();

System.out.println(recursion1(s1, s2, s3));
sc.close();

}
}

output
====
Please input the first string: this or that
Please input the second string: they’re here and they’re there
Please input the substring to find: th
is or ey’re here and atey’re ere