I am a little bit confused as to what output you want your program to have, or rather what it is supposed to represent. You don't really seem to want substrings as you also mention "ac" but you do not mention things like "cb". Would "cb" be the same as "bc" in your case?
Also (and especially if the answer to the last question is yes) how do you deal with strings like "aabbcc"?
At first sight I thought you wanted something like the following:
#include <iostream>
#include <cassert>
void print_permutations (std::string characters, const unsigned int characters_left, const unsigned int permutation_size);
int main(void)
{
std::string characters = "abcd";
for (unsigned int i = 1; i <= characters.length(); i++)
{
print_permutations(characters, i, i);
}
return 0;
}
void print_permutations (std::string characters, const unsigned int characters_left, const unsigned int permutation_size)
{
assert (permutation_size <= characters.length() && characters_left <= permutation_size);
// Recursive case
if (characters_left > 0)
{
// The index the next character will be placed at.
unsigned int current_index = permutation_size - characters_left;
// Simply Go for every character.
for (unsigned int i = current_index; i < characters.length(); i++)
{
// Swap the character at the current index with the new character
std::swap(characters[current_index], characters[i]);
// And solve the rest in recursion.
print_permutations(characters, characters_left - 1, permutation_size);
// Swap it back for the rest of the cases.
std::swap(characters[current_index], characters[i]);
}
}
// Base case
else
{
// Just print the permutation. It may be shorter then the string to
// Only print the characters of the permutation section we generated.
std::cout << characters.substr(0, permutation_size) << std::endl;
}
}
But this is not what you want right? It would output:
a
b
c
ab
ac
ba
bc
cb
ca
abc
acb
bac
bca
cba
cab
Please show which of these results is/are undesired, and more importantly: why.