Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

stringPermute.cpp (See related posts)

#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++)
{
permute(swtch(topermute, place, nextchar),
place+1);
}
}

int main(int argc, char* argv[])
{
if(argc!=2)
{
cout<<"Proper input is 'permute string'";
}
else
{
permute(argv[1], 0);
}
getchar();
return 0;
}

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets

Related Posts