Permutation of a string

Write an algorithm to generate all permutation of a string.

C++:
01 void swap(char& a, char& b)
02 {
03     char tmp=a;
04     a=b;
05     b=tmp;
06 }
07 
08 void permutation(char* a, const int k, const int m)
09 {
10     if(k==m){
11         for(int i=0;i<=m;i++)
12             cout<<a[i]<<” “;
13         cout<<endl;
14     }
15     else{
16         for(int i=k;i<=m;i++){
17             swap(a[i],a[k]);
18             permutation(a,k+1,m);
19             swap(a[i],a[k]);
20         }
21     }
22 }
Advertisements
This entry was posted in Recursive, String. Bookmark the permalink.

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