Hi all
I have been trying to write a C program which generates all possible permutations of a string (eg 123 in code below). I succeeded but it generates some garbage values after each possible permutation. Please help me in finding the possible cause. Is it something to do with initialization? Code:
#include <stdio.h>
void permute(char number[],char out[],int level,int used[]);
int main()
{
char number[] = "123";
char out[3] = "asd"; // Random initialization
int used[] = {0,0,0}; // To check if number has been used in the string output
permute(number,out,0,used);
}
void permute (char number[],char out[],int level,int used[])
{
if (level == 3)
{
printf("%s\n",out);
return;
}
int i;
for(i = 0; i < 3;i++ )
{
if( used[i] == 1) continue;
out[level] = number[i];
used[i] = 1;
permute( number, out, level + 1,used );
used[i] = 0;
}
}