Friday, 20 April 2018

write a program to print all subString of any String?

import java.util.Scanner;

public class AllSubString {

public static void main(String args[])
   {
      String s1, sub;
      int i, j;

      Scanner o1 = new Scanner(System.in);
      System.out.println("Enter a string to print it's all substrings");
      s1 = o1.nextLine();
 

      System.out.println("Substrings of \""+s1+"\" are :-");

      for( i= 0 ; i< s1.length() ; i++ )
      {
         for( j = 1 ; j<= s1.length() - i ; j++)
         {
            sub = s1.substring(i, i+j);
            System.out.println(sub);
         }
      }
   }

}

o/p:-
Enter a string to print it's all substrings
govind
Substrings of "govind" are :-
g
go
gov
govi
govin
govind
o
ov
ovi
ovin
ovind
v
vi
vin
vind
i
in
ind
n
nd
d

No comments:

Post a Comment