By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,232 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

Learn recursion

P: n/a
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.

Jun 25 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11*********************@b68g2000cwa.googlegroups. com>,
re*******@yahoo.com <re*******@yahoo.com> 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.
Jun 25 '06 #2

P: n/a
[OP hasn't shown up on news.qwest.net yet]:

| In article <11*********************@b68g2000cwa.googlegroups. com>,
| re*******@yahoo.com <re*******@yahoo.com> 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
Jun 25 '06 #3

P: n/a
re*******@yahoo.com 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
Jun 25 '06 #4

P: n/a


#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;
}


Jun 26 '06 #5

P: n/a
Kenny McCormack wrote:

In article <11*********************@b68g2000cwa.googlegroups. com>,
re*******@yahoo.com <re*******@yahoo.com> 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 |
+----------------------------------------------------------------+
Jun 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.