I'm trying to create a function with c++ that will show the permutes of a string of 3 chars. If I input a string with 3 unique chars the output is right for the first 3 permutes, but the last 3 are permutes that don't have all 3 chars. Instead the last 3 permutes are strings where one of the chars is repeated like below:
Enter a string: cat
cat
act
atc
att
att
tat
The code of the function is below. Could someone point me in the right direction?
Enter a string: cat
cat
act
atc
att
att
tat
The code of the function is below. Could someone point me in the right direction?
Rich (BB code):
void permute_string(){
char swap1;
char swap2;
int a=0; //Points to 1st char being swapped
int b=1; //Points to 2nd char being swapped
int n=3;
int swap_count=0; //keeps tracks position changes a char in has done
int char_count=0; //keeps tracks of char in the original string is being used
char givenchars[3];
char acopy[3]; // a duplicate of the original user input
cout<<"Enter a string: ";
for(int i=0; i<3; i++){
cin>>givenchars; //Store original string input into array.
acopy = givenchars; //copies original string input into a backup
}
while(char_count<3){
cout<<givenchars[0]<<givenchars[1]<<givenchars[2]<<endl;
swap1=givenchars[a];
swap2=givenchars;
//switch order
givenchars[a]=swap2;
givenchars=swap1;
if(swap_count<n){
swap_count++;
a++;
b++;
}
else{
n--;
a=0;
b=1;
char_count++;
}
}
}