Find permutation of a string

by MANIMUTHUPANDI 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
}
}
}

Tagged in:

527
like
0
dislike
0
mail
flag

You must LOGIN to add comments