Y
Yuan Zhong
hello all, not sure if it's appropriate to post here. If not, I am sorry
and please direct me to the right groups. Thanks.
I write this function to permute a string. e.g. if the input string is
"abc", I am expecting to see the output: "abc", "acb", "bac", "bca",
"cab", "cba"
there is n! output for a string of size(n). Here is my code fragment,
Can anyone points out how to improve my code so that I get the right
output. You can see the current input for just running the code. I believe
the algorithm is correct. Thanks.
void permute(char *s)
{
int i,j,k;
char str[100];
if (strlen(s) <= 1)
printf("%s\n", s);
else {
for (i=0; i< strlen(s); i++) {
memset(str, 0, sizeof(str));
printf("%c",s);
strcpy(str,s);
for (j=i; j<strlen(s); j++)
str[j] = str[j+1];
str[strlen(s)-1] = 0;
permute(str);
}
}
}
and please direct me to the right groups. Thanks.
I write this function to permute a string. e.g. if the input string is
"abc", I am expecting to see the output: "abc", "acb", "bac", "bca",
"cab", "cba"
there is n! output for a string of size(n). Here is my code fragment,
Can anyone points out how to improve my code so that I get the right
output. You can see the current input for just running the code. I believe
the algorithm is correct. Thanks.
void permute(char *s)
{
int i,j,k;
char str[100];
if (strlen(s) <= 1)
printf("%s\n", s);
else {
for (i=0; i< strlen(s); i++) {
memset(str, 0, sizeof(str));
printf("%c",s);
strcpy(str,s);
for (j=i; j<strlen(s); j++)
str[j] = str[j+1];
str[strlen(s)-1] = 0;
permute(str);
}
}
}