-
function overlaying?
i ran into trouble with this code.
Code:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void swap(char *, int, int);
void permut(char *string, int k); //k = number of chars
int main ()
{
char a[] = "abc";
permut(a, 0);
return 0;
}
void permut(char *string, int k)
{
int i;
if (k == strlen(string))
printf("%s\n", string);
else
{
for (i=k;i<strlen(string);i++)
{
swap(string, k, i);
permut(string, k+1);
}
}
}
void swap(char *string, int k, int i)
{
char hold;
hold = string[k];
string[k] = string[i];
string[i] = hold;
}
thats maybe because it isnt mine alone.
the program works fine but i dont really understand the codeflow.
the for statement in the permut function doesnt make much sense to me because the first time it is called, i is set to zero.(value of k right?). the swap function gets called by permut. but isnt the only thing swap is doing setting string[0] = string[0] and so forth....?
before i forget about it, whats about that permut function?
it calls itself a few times and terminates with printing the string.
why is the string printed more than once, when actually it terminates itself after having printed the string once?
is the function permut overlaying itself?
help would be appreciated. thanks
-
thanks that helped me out understanding this program a little.
im sure there are different possibilities to write such a program right? if yes i will code my own different one. :cool: