Program to print all substrings of a given string | * C ++ program to print all possible * substrings of a given string * without checking for duplication . ; * Function to print all ( n * ( n + 1 ) ) / 2 * substrings of a given string s of length n . ; * Fix start index in outer loop . * Reveal new character in inner loop till end of string . * Print till - now - formed string . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAllSubstrings ( string s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { char temp [ n - i + 1 ] ; int tempindex = 0 ; for ( int j = i ; j < n ; j ++ ) { temp [ tempindex ++ ] = s [ j ] ; temp [ tempindex ] = ' \0' ; printf ( " % s STRNEWLINE " , temp ) ; } } } int main ( ) { string s = " Geeky " ; printAllSubstrings ( s , s . length ( ) ) ; return 0 ; }