c++ program to print permutations of a word

Posted: July 3, 2012 in Programming
Tags: , , , ,

 

#include “iostream”
#include “string”

using namespace std;

void comb(string,int,int);
void print_word(string);
void swap(string&,int,int);
void rotate_left(string&,int,int);
string word;
int main()
{
cout<>word;
int len=word.length();
comb(word,0,len);
cin.get();
cin.get();
return 0;
}

void comb(string w, int start, int n)
{
print_word(w);
if(start=start;i–)
{
for(j=i+1;j<n;j++)
{
swap(w,i,j);
comb(w,i+1,n);
}

rotate_left(w,i,n);

}
}

}

void print_word(string w)
{
cout<<w<<endl;
}

void swap(string &w,int i,int j)
{
char t;
t=w[i];
w[i]=w[j];
w[j]=t;
}

void rotate_left(string &w,int go,int n)
{
char tmp=w[go];
for(int i=go;i<n-1;i++)
{
w[i]=w[i+1];
}
w[n-1]=tmp;

}

 

Output : Enter a string :abcd
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s