Print all substrings of a given string

Objec­tive:  Given a string write an algorithm to print all the possible sub strings.


String input = “abcd”;
Output: Possible sub strings –

a          a b      a b c   a b c d
b          b c      b c d
c          c d

  • Use nested loops.
  • Outer loops will decide the starting point.
  • First inner loops will decide the group size. Starting from 1 and goes up string character array size.
  • Most inner loop will create the sub strings and print it.
  • See the code below for more understanding.

Complete Code: <


a b
a b c
a b c d
b c
b c d
c d

Top Companies Interview Questions..-


If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

1 Response

  1. Sourabh Snehalata Jain says:

    We can exclude one extra for loop

    for (c = 0; c < length; c++)
    for(i = 1; i <= length – c; i++)
    sub = string.substring(c, c+i);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: