Write a program to find all substrings of a string.

devquora
devquora

Posted On: Dec 24, 2020

 

The JAVA program to find all substrings of a 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);
         }
      }
   }
}

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Amazon Support Engineer Interview Questions

    Explain What is STP?

    The STP also stands for Spanning Tree Protocol constructs a loop-free logical topography for Ethernet networks. It is a ..

    Amazon Support Engineer Interview Questions

    Write a function to check if a number is prime?

    A prime number is a whole number greater than 1, which is only divisible by 1 and itself. // function check whether a number // is prime or not bool isPrime(int n) { // Corner case if (n...

    Amazon Support Engineer Interview Questions

    Write a regular expression to validate phone number in  XXX-XXX-XXXX format?

    A regular expression to validate phone number in XXX-XXX-XXXX format is follows: /^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/...