Java Program to Find SubStrings in a String

This Java program is to find out and display the sub strings from any  strings which the user enters in the program. The source code in Java programming language of finding the substrings from a given string is given below :

Java Program Source Code to Find Sub string

import java.util.Scanner;

class SubstringsOfAString
{
   public static void main(String args[])
   {
      String string, sub;
      int i, c, length;

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

      length = string.length();

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

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

Output of Java Program for finding Sub Strings

0 comments:

Post a Comment