I have written a program which prints all the permutations of a given string. But it was printing some weird things. The code goes as follows:
#include <stdio.h>
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%d\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j));
}
}
}
int main(void)
{
char a[100];
gets(a);
int k;
k=strlen(a);
permute(a, 0, k-1);
system("pause");
}
It was printing some numbers instead of given string.. plz help
gets. Terrible things will happen if someone types more than 100 characters.