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