Question

Write a method that adds all subsets of the letters in its first argument, str, to...

Write a method that adds all subsets of the letters in its first argument, str, to the ArrayList that is its second argument. For example, generateSubstrings("ABC", result) adds to result the strings: "", "A", "B", "C", "AB", "AC", "BC'', "ABC" The order of the strings does not matter. In your test code, you will probably want a helper method that prints all of the strings in an 2 ArrayList. You may use iteration in any way you see fit for this problem. Do not worry too much about eliminating all memory allocation in this method. Demonstrate that your program works by printing to the console the source string and the subset, followed by the result. Call the primary source file SubstringFinder.java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code. Please comment if you want any change or if you face any difficulty.

Code:

=====

import java.util.ArrayList;
import java.util.Scanner;

public class SubstringFinder {
   static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   System.out.print("Enter string: ");
       String str = sc.nextLine();
findSubsets(str, list);
System.out.println("Below are the subsets: ");
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
  
private static void findSubsets(String str, ArrayList<String >list){
   int length = str.length();   
for(int i = 0; i < length; i++) {
for(int j = i; j < length; j++) {
list.add(str.substring(i, j+1));
}
}
}
}

Output screenshot:

=================

Add a comment
Know the answer?
Add Answer to:
Write a method that adds all subsets of the letters in its first argument, str, to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment...

    Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment 1 ID: 1. True / False 2. True / False 3. True / False 4. True / False 5. True / False 6. True / False 7. True / False 8. True / False 9. True / False 10. True / False Variable and functions identifiers can only begin with alphabet and digit. Compile time array sizes can be non-constant variables. Compile time array...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT