All Substrings

 

For a given input string(str), write a function to print all the possible substrings.

Sample Input 1:
abc
Sample Output 1:
a 
ab 
abc 
b 
bc 
c 
import java.util.Scanner;

public class AllSubstring {

	public static void main(String[] args) {
	
		Scanner s = new Scanner(System.in);
		String st = s.nextLine();
		printSubstrings(st);
	}
	
	
	public static void printSubstrings(String str) {
                int l=str.length();
		for(int i=0;i<l-1;i++)
		{
			for(int j=i;j<l;j++)
			{
				System.out.println(str.substring(i,j+1));
			}
		}
		System.out.println(str.substring(l-1));
}
}

        

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence