Java Program To Find All substring Of String

01. Example:

import java.util.Scanner;

class findSubstringsOfAString {
    public static void main(String args[]) {
        
        String string, sub;
        int i, counter, length;
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter A String To Print All Substrings: ");
        string = in .nextLine();
        length = string.length();
        System.out.println("Substrings Of "" + string + "" Are");
        
        for (counter = 0; counter < length; counter++) {
            for (i = 1; i <= length - counter; i++) {
                sub = string.substring(counter, counter + i);
                System.out.println(sub);
            }
        }
        
    }
}

Output:

Enter A String To Print All Substrings: Zeroones
Substrings Of "Zeroones" Are
Z
Ze
Zer
Zero
Zeroo
Zeroon
Zeroone
Zeroones
e
er
ero
eroo
eroon
eroone
eroones
r
ro
roo
roon
roone
roones
o
oo
oon
oone
oones
o
on
one
ones
n
ne
nes
e
es
s

Leave a comment