I am trying to run the following code for finding out permutations of a string:
void swapchar(char *a,char *b)
{
char tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void permute (char *a,int i,int n)
{
if (i==n)
{
for (int k=0;k<(sizeof(a)/sizeof(a[0]));k++)
{
printf("\t%c",a[k]);
}
printf("\n");
}
else
{
for (int j=i;j<n;j++)
{
swapchar((a+i),(a+j));
permute(a, i+1, n);
swapchar((a+i),(a+j));
}
}
}
The output I get is something like:
K N L U A <inverted ?> <inverted ?> <inverted ?>
K A N U L <inverted ?> <inverted ?> <inverted ?>
K A N L U <inverted ?> <inverted ?> <inverted ?>
K A U N L <inverted ?> <inverted ?> <inverted ?>
K A U L N <inverted ?> <inverted ?> <inverted ?>
K A L U N <inverted ?> <inverted ?> <inverted ?>
Can you please explain to me where is my code going South that I am getting these junk characters at the end?