Signup/Sign In
PUBLISHED ON: MARCH 11, 2021

Java Program To Find all the Subsets of a String

In this tutorial, we will learn how to print all the subsets of a string. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: WORLD

Output: the subsets of the entered string is:

W

O

R

L

D

WO

OR

RL

LD

WOR

ORL

RLD

WORL

ORLD

WORLD

Program 1: Find all the Subsets of a String

In this program, we will use three nested for loops to print all the subsets of a string. The first for loop is used to select the starting element, the second for loop is required to select the ending element, and the third for loop is used to print the selected element from starting to ending elements.

Algorithm

  1. Start
  2. Declare a string.
  3. Ask The user to initialize the string
  4. Convert it to a character array.
  5. Call a method that will find all the subsets of a string.
  6. Use three for loops for the same.
  7. Use the first for loop to select the starting index of the subset.
  8. Use the second for loop to hold the ending index of the subset.
  9. Use the third for loop to print all the subsets.
  10. Stop.

Below is the code for the same in Java language.

//Java Program to Find all the subsets of a string
import java.util.*;
public class Main
{
    //To find all the subsets of a string
   static void subString(char str[], int n)
   {
        // To select starting point
        for (int t = 1; t <= n; t++) 
        {
            // To select ending point
            for (int i = 0; i <= n - t; i++) 
            {
                //  Print characters from selected
                // starting to end point.  
                int j = i + t - 1;
                for (int k = i; k <= j; k++) 
                {
                    System.out.print(str[k]);
                }
 
                System.out.println();
            }
        }
    } 
    // Driver program to test above function
    public static void main(String[] args) 
    {
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string is "+str1);
        String str1=sc.nextLine();
        char str[] = str1.toCharArray();
        System.out.println("All the substrings of the above string are: ");
        subString(str, str.length);
    }
}


Enter the string: Code
All the substrings of the above string are:
C
o
d
e
Co
od
de
Cod
ode
Code

Program 2: Find all the Subsets of a String

In this program, we will use substr() method to print all the subsets of the given string. The str.substr(i,j) will print the substring of length j starting from index i in the string.

Algorithm

  1. Start
  2. Declare a string.
  3. Initialize it.
  4. Call a method to find all the subsets of the entered string.
  5. Pass the string and string length as parameters.
  6. Use two for loops for the same.
  7. Use the first for loop to select the starting index of the subset.
  8. Use the second for loop to hold the ending index of the subset.
  9. Print all the subsets.
  10. Stop.

Below is the code for the same in Java language.

//Java Program to Find all the subsets of a string
public class Main
{
    //To find all the subsets of a string
   static void subString(String str, int n)
   {
        for (int i = 0; i < n; i++)      //To select the starting index
        {
           for (int j = i+1; j <= n; j++)    //To select the ending index
            {
                System.out.println(str.substring(i, j));
            }
        }
    }
    // Driver program to test above function
    public static void main(String[] args) 
    {
        String str="Hello";
        System.out.println("The entered string are "+str);
        System.out.println("All the substrings of the above string is: ");
        //Call to find the all the subsets of the string
        subString(str, str.length());
    }
}


The entered string is Hello
All the substrings of the above string are:
H
He
Hel
Hell
Hello
e
el
ell
ello
l
ll
llo
l
lo
o



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.