--- In [EMAIL PROTECTED], "Trilokesh"
<[EMAIL PROTECTED]> wrote:
> 
> 
> Hi all,
> 
> I want to write a program,which will find all the
> "permutation" of a given string.
> please help in solving problem,is this problem can
> be solve by recursion.
/*   ----------------------------------------------------   */
/*   Nth Permutation Generator.                             */
/*   Copyright (c) LJW December 4, 2000.                    */
/*   This is based on the permutation generator that was    */
/*   described in the notes published by L. J. Woodrum      */
/*   in 1970.                                               */
/*   ----------------------------------------------------   */
#include  <stdlib.h>
#include  <stdio.h>
#include  <string.h>
int  factorial(int N)                                       {
int  i, n;
for (i = 1, n = N; n > 0; n--)     i = i * n;
return(i);
                                                            }
/*   -----------------------------------------------------  */
/*   Given L, the length of the permutation, N, the number  */
/*   of the permutation N in the list, compute permutation  */
/*   number N at the int array P.                           */
/*   -----------------------------------------------------  */
void perm(int L, int N, int *P)                             {
int  i,j,n;
if (L <= 1)                 P[0] = 0;
     return;
                                                       }
/*   -----------------------------------------------------  */
/*   Compute the mixed radix representation of the number N */
/*   in the radix L, L-1, ... 2, 1.                         */
/*   Do this by repeatedly dividing by 1, 2, 3, .. to L-1.  */
/*   -----------------------------------------------------  */
P[L - 1] = 0;
for (i = L-2, n = 1; i >= 0; i--)                      {
     n = n + 1;
     P[i] = N % n;
     N = N / n;
                                                       }

/*   -----------------------------------------------------  */
/*   Now generate the permutation at the array P.           */
/*   -----------------------------------------------------  */
for(i = L - 1; i > 0; )                                {
     i--;
     n = P[i];
     for (j = i + 1; j < L; j++)                  {                 
        if (n <= P[j]) P[j]++;
                                                  }
                                                       }
                                                            }
int  main(int  argc, char **argv)                           {
int       P[16];
int       i,f;
int       L;
int       N;
#define   Uchr unsigned char
Uchr      string[128];
Uchr      *str;
str = &string[0];
if (argc < 2)                                          {
fprintf(stderr, "Not enough arguments.  Syntax: perms
character_string\n");
          exit(7);
                                                       }
L = strlen(argv[1]);
str[L] = '\0';
f = factorial(L);
for (N = 0; N < f; N++)                           {
// Going to get the mixed radix representation for length %d and
//permutation #%d\n",L,N);
perm(L,N,P);
for (i = 0; i < L; i++)                      {
     str[i] = (argv[1])[P[i]];
                                             }
     fprintf(stdout, "%s\n", str);

                                                  }

return(0);
                                                              }








------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe : [EMAIL PROTECTED]

 
Yahoo! Groups Links

<*> To reply to this message, go to:
    http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=3619
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/Programmers-Town/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to