Need help with my java programming homework please! Thanks for your help. Implem
ID: 3837102 • Letter: N
Question
Need help with my java programming homework please! Thanks for your help.
Implement a class called SubstringTester that uses recursion to generate all substrings of a given String. For example, the substrings of the string "Sluggo" are the 22 strings:
You are not allowed to use any loops to build the substrings -- at least in your final product. You may, however, find it productive to solve the problem using loops first, and then translate to recursive code.
Implement methods in SubstringTester that will interact with the user, acquire a phrase and produce all of the substrings of that phrase. This class should:
Get a string from the user (possibly in ‘main’)
Generate and display the substrings of the phrase
Repeat the above two steps until the user chooses to quit
Explanation / Answer
import java.util.Scanner;
public class SubstringTester {
private static void printAllSubstring(int start, int end, String s)
{
if(start == s.length() && end == s.length()){
return;
}
else
{
if(end == s.length()+1)
{
printAllSubstring(start+1,start+1,s);
}
else
{
System.out.println(s.substring(start, end));
printAllSubstring(start, end+1,s);
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to generate its all substring: ");
String str = sc.next();
printAllSubstring(0, 1, str);
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.