Yes, there is. You need to use the #algorithm library in order to use the next_permutation(string.begin(), string.end()) function
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
int main(){
std::string input; /*or simply string if 'using namespace std;'*/
std::cout << "insert text: ";
getline(std::cin, input); /*getline is more fit to read strings input*/
while(next_permutation(input.begin(), input.end()){
std::cout << input << std::endl;
}
}
next_permutation returns a boolean that says if there is a next permutation or not, which allows you to create a loop to do all the permutations.
You'll see though, that the permutations aren't in alphanumeric order. In order to correct this, you must first alphabetically order your string. For example, my name is 'caio', so in order to get the organized set of permutations, i'd first need to turn 'caio' in 'acio'.
You can do that with some built-in function I guess, but you can also create your own algorithm
string swap(string text, int index){ /*swap letters*/
char aux;
aux = text[index];
text[index] = text[index-1];
text[index-1] = aux;
return text;
}
string bubble_sort(string text){ /*bubble sort is an O(n²) sorting algorithm. It'll do the trick*/
for(int i = 0; i < text.size()-1; i++){
for(int j = text.size()-1; j > 0; j--){
if(text[j] < text[j-1]){
text = swap(text, j);
}
}
}
text.size() returns the size of the string, but the string count starts at the 0 index. so if you try to access string[string.size()] you'll get the null terminator. j>0 cause if j>=0, then j-1 will access an invalid index.
Even though though question is 4 years old, I came across this problem this week and got introduced to such methods. If you want to learn more about how permutations work, I recommend this video: https://www.youtube.com/watch?v=GuTPwotSdYw&ab_channel=Techdose