Learn recursion

R

red_hax0r

I'm trying to learn how to make a scrambling algorithm in C that will
turn out all possible permutations of a word without repeating (but
repeating can easily be fixed). I found out a few algorithms in
comp.lang.c++, but none of them work.

I'm using gcc.
 
K

Kenny McCormack

I'm trying to learn how to make a scrambling algorithm in C that will
turn out all possible permutations of a word without repeating (but
repeating can easily be fixed). I found out a few algorithms in
comp.lang.c++, but none of them work.

In order to understand recursion, you must first understand recursion.
 
M

Morris Dovey

[OP hasn't shown up on news.qwest.net yet]:

| In article <[email protected]>,
|| I'm trying to learn how to make a scrambling algorithm in C that
|| will turn out all possible permutations of a word without
|| repeating (but repeating can easily be fixed). I found out a few
|| algorithms in comp.lang.c++, but none of them work.

Recursion is fun. I came up with the following; but you'll need to
clean it up a bit because I didn't check the return value from
malloc() and left a debug printf() in place so you can better see
what's going on during execution.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void permute(char *fixed,char *remain)
{ unsigned i,len = strlen(fixed);
char *newrem;
unsigned char c;

if (strlen(remain))
{ newrem = malloc(1+strlen(remain));
fixed[len+1] = '\0';
for (i=0; i<strlen(remain); i++)
{ fixed[len] = remain;
strcpy(newrem,remain);
strcpy(newrem+i,remain+i+1);
printf("permute(\"%s\",\"%s\")\n",fixed,newrem);
permute(fixed,newrem);
}
fixed[len] = '\0';
}
else puts(fixed);
}
int main(void)
{ char left[16]="\0",right[16]="ABCDE";
permute(left,right);
return 0;
}

Hope this helps to get you started (I'm sure there's a better way.)
 
R

red_hax0r

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void permute(char *fixed,char *remain)
{ unsigned i,len = strlen(fixed);
char *newrem;
unsigned char c;

if (strlen(remain))
{ newrem = malloc(1+strlen(remain));
fixed[len+1] = '\0';
for (i=0; i<strlen(remain); i++)
{ fixed[len] = remain;
strcpy(newrem,remain);
strcpy(newrem+i,remain+i+1); <<<<


Yeah but what does this line do? Why shoudl it be necessary? I know
it is, because it spit out garbage without it. I tried changing it to
address notation and it works. Any tutorials you could suggest would
be helpful.
printf("permute(\"%s\",\"%s\")\n",fixed,newrem);
permute(fixed,newrem);
}
fixed[len] = '\0';
}
else puts(fixed);
}
int main(void)
{ char left[16]="\0",right[16]="ABCDE";
permute(left,right);
return 0;
}
 
C

Charles Richmond

Kenny said:
In order to understand recursion, you must first understand recursion.
Ah-hhmmm. In order to understand recursion, you must first
understand a *simpler* form the of the recursion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top