C program to find all the permutations of the string

Program:

#include<stdio.h>
#include<string.h>
void swap(char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
void permute(char *a,int l,int r)
{
    if(l==r)
    {
        printf("%s \n",a);
    }
    else
   {
       for (int i = l; i <= r; i++)
       {
            swap((a+l),(a+i));
            permute(a,l+1,r);
            swap((a+l),(a+i));
       }
   }
}
int main()
{
    char str[]="car";
    int l=strlen(str);
    permute(str,0,l-1);
}


Output:

car                                                                                                                                            
cra                                                                                                                                            
acr                                                                                                                                            
arc                                                                                                                                            
rac                                                                                                                                            
rca          



Comments

Popular posts from this blog

c++ program to print string along the diagonals of the matrix.

Kollywood Game

C program to print the following Pattern