Print all possible strings that can be made by placing spaces | C ++ program to print permutations of a given string with spaces . ; Function recursively prints the strings having space pattern . i and j are indices in ' str [ ] ' and ' buff [ ] ' respectively ; Either put the character ; Or put a space followed by next character ; This function creates buf [ ] to store individual output string and uses printPatternUtil ( ) to print all permutations . ; Buffer to hold the string containing spaces 2 n - 1 characters and 1 string terminator ; Copy the first character as it is , since it will be always at first position ; Driver program to test above functions
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void printPatternUtil ( const char str [ ] , char buff [ ] , int i , int j , int n ) { if ( i == n ) { buff [ j ] = ' \0' ; cout << buff << endl ; return ; } buff [ j ] = str [ i ] ; printPatternUtil ( str , buff , i + 1 , j + 1 , n ) ; buff [ j ] = ' ▁ ' ; buff [ j + 1 ] = str [ i ] ; printPatternUtil ( str , buff , i + 1 , j + 2 , n ) ; } void printPattern ( const char * str ) { int n = strlen ( str ) ; char buf [ 2 * n ] ; buf [ 0 ] = str [ 0 ] ; printPatternUtil ( str , buf , 1 , 1 , n ) ; } int main ( ) { const char * str = " ABCD " ; printPattern ( str ) ; return 0 ; }