Print all subsequences of a string | CPP program to generate power set in lexicographic order . ; str : Stores input string n : Length of str . curr : Stores current permutation index : Index in current permutation , curr ; base case ; backtracking ; Generates power set in lexicographic order . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSubSeqRec ( string str , int n , int index = -1 , string curr = " " ) { if ( index == n ) return ; if ( ! curr . empty ( ) ) { cout << curr << " STRNEWLINE " ; } for ( int i = index + 1 ; i < n ; i ++ ) { curr += str [ i ] ; printSubSeqRec ( str , n , i , curr ) ; curr = curr . erase ( curr . size ( ) - 1 ) ; } return ; } void printSubSeq ( string str ) { printSubSeqRec ( str , str . size ( ) ) ; } int main ( ) { string str = " cab " ; printSubSeq ( str ) ; return 0 ; }