Mar 2, 2022

Print all permutations of a string

#include <iostream>
#include <cstring>
#include <string>
#define MAXSIZE 256
#define PATSIZE 10
using namespace std;

void permute(char str[], int l, int r)
{
    
    if(l==r)
    {
        cout&lt;&lt; str &lt;&lt; endl;
        return;
    }    
   
    
    for(int i=l;i&lt;=r;i++)
    {
        swap(str[l], str[i]);

        permute(str, l+1, r);
        
        swap(str[l], str[i]);      
    }

}

int main() {
// your code goes here
char txt[] = "abcd";
permute(txt, 0, strlen(txt)-1);
return 0;
}

No comments: