Learn recursion

Discussion in 'C Programming' started by red_hax0r@yahoo.com, Jun 25, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. In article <>,
    <> 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.
     
    Kenny McCormack, Jun 25, 2006
    #2
    1. Advertising

  3. Morris Dovey Guest

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

    | In article <>,
    | <> 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;
    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
     
    Morris Dovey, Jun 26, 2006
    #3
  4. 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
     
    Frederick Gotham, Jun 26, 2006
    #4
  5. Guest


    >
    > #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;
    > }
    >
     
    , Jun 26, 2006
    #5
  6. Kenny McCormack wrote:
    >
    > In article <>,
    > <> 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 |
    +----------------------------------------------------------------+
     
    Charles Richmond, Jun 26, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Mohler
    Replies:
    1
    Views:
    449
    Steve Grazzini
    Sep 16, 2003
  2. Porky Pig Jr
    Replies:
    3
    Views:
    1,077
    Fuzzyman
    May 12, 2004
  3. CoreyWhite
    Replies:
    1
    Views:
    675
    JohnQ
    Mar 31, 2007
  4. Alexander
    Replies:
    20
    Views:
    1,072
    BGB / cr88192
    Sep 11, 2010
  5. Replies:
    8
    Views:
    760
    John Reye
    Apr 26, 2012
Loading...

Share This Page