473,479 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Learn recursion

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
5 1418
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
[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
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


#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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3395
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
2735
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
4115
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
1549
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
5535
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
2256
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
3696
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
20
2957
by: athar.mirchi | last post by:
..plz define it.
35
4681
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.