Find permutation of a string
by MANIMUTHUPANDI[ Edit ] 2012-08-28 12:55:19
#include "stdio.h"
void main()
{
char *str1;
printf("Enter the string that U want to permutate ");
gets(str1);
permute(str1, 0,strlen(str1));
}
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int first, int last)
{
int j;
j=last-first;
if (j== 1)
printf("%sn", a);
else
{
for (int i = 0; i <last; i++)
{
swap((a[first]), (a[i]));
permute(a, first+1, last);
swap((a[first]), (a[i]));
//backtrack
}
}
}