Use recursion to implement a method public static boolean fi

Use recursion to implement a method public static boolean find(String text, String str) that tests whether a given text contains a string. For example, find (\"Mississippi\", \"sip\") returns true.

Solution

SearchStringTest.java

import java.util.Scanner;


public class SearchStringTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the string: \");
       String s = scan.nextLine();
       System.out.print(\"Enter the substring: \");
       String sub = scan.nextLine();
       System.out.println(find(s,sub));
   }
   public static boolean find(String text, String str){

       if ((text == null) || (str == null) || text.isEmpty()) {
       return false;
       } else if (text.startsWith(str)) {
       return true;
       } else {
       return find(text.substring(1), str);
       }
   }

}

Output:

Enter the string: Mississippi
Enter the substring: sip
true

 Use recursion to implement a method public static boolean find(String text, String str) that tests whether a given text contains a string. For example, find (\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site