Program to print all substrings of a given string | CPP program for the above approach ; finding the length of the string ; outermost for loop this is for the selection of starting point ; 2 nd for loop is for selection of ending point ; 3 rd loop is for printing from starting point to ending point ; changing the line after printing from starting point to ending point ; Driver Code ; calling method for printing substring
#include <iostream> NEW_LINE using namespace std ; void printSubstrings ( string str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) { cout << str [ k ] ; } cout << endl ; } } } int main ( ) { string str = " abcd " ; printSubstrings ( str ) ; return 0 ; }