Velocity Reviews > Learn recursion

# Learn recursion

red_hax0r@yahoo.com
Guest
Posts: n/a

 06-25-2006
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.

Kenny McCormack
Guest
Posts: n/a

 06-25-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>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.

Morris Dovey
Guest
Posts: n/a

 06-25-2006
[OP hasn't shown up on news.qwest.net yet]:

| In article <(E-Mail Removed). com>,
| (E-Mail Removed) <(E-Mail Removed)> wrote:
|| 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[i];
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.)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto

Frederick Gotham
Guest
Posts: n/a

 06-25-2006
(E-Mail Removed) posted:

> 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.

If you're using C++, check this out:

http://en.wikipedia.org/wiki/Template_metaprogramming

--

Frederick Gotham

red_hax0r@yahoo.com
Guest
Posts: n/a

 06-26-2006

>
> #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[i];
> 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;
> }
>

Charles Richmond
Guest
Posts: n/a

 06-26-2006
Kenny McCormack wrote:
>
> In article <(E-Mail Removed). com>,
> (E-Mail Removed) <(E-Mail Removed)> wrote:
> >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.
>

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

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM Alexander C Programming 20 09-11-2010 01:04 AM CoreyWhite C++ 1 03-31-2007 08:56 PM Porky Pig Jr Python 3 05-12-2004 08:58 AM Tim Mohler Perl 1 09-16-2003 12:35 AM

Advertisments