Program to print all substrings of a given string

Given a string as an input. We need to write a program that will print all non-empty substrings of that given string.

Examples :

Input :  abcd
Output :  a 
          b
          c
          d
          ab
          bc
          cd
          abc
          bcd
          abcd



We can run three nested loops, the outermost loop picks starting character, mid loop considers all characters on right of the picked character as ending character of substring. The innermost loop prints characters from currently picked starting point to picked ending point.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all possible
// substrings of a given string
  
#include<bits/stdc++.h>
using namespace std;
  
// Function to print all sub strings
void subString(char str[], int n) 
{
    // Pick starting point
    for (int len = 1; len <= n; len++) 
    {    
        // Pick ending point
        for (int i = 0; i <= n - len; i++) 
        {
            //  Print characters from current
            // starting point to current ending
            // point.  
            int j = i + len - 1;            
            for (int k = i; k <= j; k++) 
                cout << str[k];
              
            cout << endl;
        }
    }
}
  
// Driver program to test above function
int main() 
{
    char str[] = "abc";
    subString(str, strlen(str));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to print all possible
// substrings of a given string
  
class GFG {
  
// Function to print all sub strings
    static void subString(char str[], int n) {
        // Pick starting point
        for (int len = 1; len <= n; len++) {
            // Pick ending point
            for (int i = 0; i <= n - len; i++) {
                //  Print characters from current
                // starting point to current ending
                // point.  
                int j = i + len - 1;
                for (int k = i; k <= j; k++) {
                    System.out.print(str[k]);
                }
  
                System.out.println();
            }
        }
    }
  
// Driver program to test above function
    public static void main(String[] args) {
        char str[] = {'a', 'b', 'c'};
        subString(str, str.length);
  
    }
}
// This code is contributed by PrinciRaj1992

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print all possible
# substrings of a given string
  
  
# Function to print all sub strings
def subString(Str,n):
      
    # Pick starting point
    for Len in range(1,n + 1):
          
        # Pick ending point
        for i in range(n - Len + 1):
              
            # Print characters from current
            # starting point to current ending
            # point. 
            j = i + Len - 1
  
            for k in range(i,j + 1):
                print(Str[k],end="")
            print()
              
# Driver program to test above function
Str = "abc"
subString(Str,len(Str))
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all possible
// substrings of a given string
using System;
  
public class GFG {
  
    // Function to print all sub
    // strings
    static void subString(string str, 
                               int n) 
    {
          
        // Pick starting point
        for (int len = 1; len <= n;
                               len++) 
        
            // Pick ending point
            for (int i = 0; 
                    i <= n - len; i++) 
            {
                // Print characters
                // from current
                // starting point to
                // current ending
                // point. 
                int j = i + len - 1;
                  
                for (int k = i; k <= j;
                                    k++) 
                    Console.Write(str[k]);
                  
                Console.WriteLine();
            }
        }
    }
      
    // Driver program to test
    // above function
    static public void Main ()
    {
        string str = "abc";
        subString(str, str.Length);
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print all possible
// substrings of a given string
  
// Function to print all sub strings
function subString($str, $n
{
      
    // Pick starting point
    for ($len = 1; $len <= $n; $len++) 
    
          
        // Pick ending point
        for ($i = 0; $i <= $n - $len; $i++) 
        {
              
            // Print characters from current
            // starting point to current ending
            // point. 
            $j = $i + $len - 1;         
            for ($k = $i; $k <= $j; $k++) 
                echo $str[$k];
              
            echo "\n";
        }
    }
}
  
    // Driver Code
    $str = "abc";
    subString($str, strlen($str));
      
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

a
b
c
ab
bc
abc

Method 2 (Using substr() function)
s.substr(i, len) prints substring of length ‘len’ starting from index i in string s.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all possible
// substrings of a given string
#include<bits/stdc++.h>
using namespace std;
  
// Function to print all sub strings
void subString(string s, int n) 
{
    // Pick starting point in outer loop
    // and lengths of different strings for
    // a given starting point
    for (int i = 0; i < n; i++) 
        for (int len = 1; len <= n - i; len++)
            cout << s.substr(i, len) << endl;
}
  
// Driver program to test above function
int main() 
{
    string s = "abcd";
    subString(s,s.length());
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all substrings of a string
public class GFG {
  
    // Function to print all substring
    public static void SubString(String str, int n)
    {
       for (int i = 0; i < n; i++) 
           for (int j = i+1; j <= n; j++)
             
                // Please refer below article for details
                // of substr in Java
                System.out.println(str.substring(i, j));
    }
  
    public static void main(String[] args)
    {
        String str = "abcd";
        SubString(str, str.length());
    }
}
  
// This code is contributed by ASHISH KUMAR PATEL

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

      
# Python program to print all possible
# substrings of a given string
   
# Function to print all sub strings
def subString(s, n):
    # Pick starting point in outer loop
    # and lengths of different strings for
    # a given starting point
    for i in range(n):
        for len in range(i+1,n+1):
            print(s[i: len]);
  
# Driver program to test above function
s = "abcd";
subString(s,len(s));
  
# This code is contributed by princiraj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all substrings of a string
using System; 
  
public class GFG { 
  
    // Function to print all substring
    public static void SubString(String str, int n)
    {
    for (int i = 0; i < n; i++) 
        for (int j = 1; j <= n - i; j++)
              
                // Please refer below article for details
                // of substr in Java
                Console.WriteLine(str.Substring(i, j));
    }
  
    public static void Main()
    {
        String str = "abcd";
        SubString(str, str.Length);
    }
}
  
/*This code is contributed by PrinciRaj1992*/

chevron_right



Output:

a
ab
abc
abcd
b
bc
bcd
c
cd
d

This method is contributed by Ravi Shankar Rai

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


21


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.