SubString Problem in C

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

// 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;
}

Java

// 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
// https://www.geeksforgeeks.org/java-lang-string-substring-java/
System.out.println(str.substring(i, j));
}

public static void main(String[] args)
{
String str = “abcd”;
SubString(str, str.length());
}
}