Code:
#include <iostream>
#include <string>
using namespace std;
string swtch(string topermute, int x, int y)
{
string newstring = topermute;
newstring[x] = newstring[y];
newstring[y] = topermute[x]; //avoids temp variable
return newstring;
}
void permute(string topermute, int place)
{
if(place == topermute.length() - 1)
{
cout << topermute << endl;
}
for(int nextchar = place; nextchar < topermute.length(); nextchar++)
{
cout << place << endl; //how can place be 1 after being 2. it is never decremented.
permute(swtch(topermute, place, nextchar), place+1);
}
}
int main()
{
string str = "cat";
permute(str, 0);
}