Program to print all sub strings of a String

By | August 26, 2017

This is a program in Java to print all the sub strings of a input string.

Example : The list of all substrings of the string “apple” would be “apple“, “appl“, “pple“, “app“,
ppl“, “ple“, “ap“, “pp“, “pl“, “le“, “a“, “p“, “l“, “e“, “”.

import java.util.Scanner;

public class SubStrings{

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str = scan.nextLine();

		// print all substrings 
		for (int c = 0; c < str.length(); c++) {
			for (int i = 1; i <= str.length() - c; i++) {		
				String sub = str.substring(c, c + i);
				System.out.println(sub);
				}
			}
		}
}