Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
print all permutations of string
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  18 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
anurag  
View profile  
 More options Jul 20 2006, 1:57 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "anurag" <anurag1...@gmail.com>
Date: 20 Jul 2006 10:57:56 -0700
Local: Thurs, Jul 20 2006 1:57 pm
Subject: print all permutations of string
hey can anyone help me in writing a code in c (function) that prints
all permutations of a string.please help

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor Bazarov  
View profile  
 More options Jul 20 2006, 1:59 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "Victor Bazarov" <v.Abaza...@comAcast.net>
Date: Thu, 20 Jul 2006 13:59:33 -0400
Local: Thurs, Jul 20 2006 1:59 pm
Subject: Re: print all permutations of string

anurag wrote:
> hey can anyone help me in writing a code in c (function) that prints
> all permutations of a string.please help

Please, for C questions do not cross-post to comp.lang.c++.  Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Heathfield  
View profile  
 More options Jul 20 2006, 2:13 pm
Newsgroups: alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
Followup-To: comp.lang.c
From: Richard Heathfield <inva...@invalid.invalid>
Date: Thu, 20 Jul 2006 18:13:49 +0000
Local: Thurs, Jul 20 2006 2:13 pm
Subject: Re: print all permutations of string
[comp.lang.c++ snipped - followups set to comp.lang.c]

anurag said:

> hey can anyone help me in writing a code in c (function) that prints
> all permutations of a string.please help

Consider how you would do this by hand. For example, let's look at the
string "ABCD". We can think of the permutations of this string as being
divided into four sets (because the string is four characters long):

1) All the strings starting with A and continuing with some permutation of
BCD
2) All the strings starting with B and continuing with some permutation of
CDA
3) All the strings starting with C and continuing with some permutation of
DAB
4) All the strings starting with D and continuing with some permutation of
ABC

As you might imagine, you can get at these sets simply by moving the string
around a little.

Let's look at just one of these sets (because the others are dealt with in
the same way, obviously): strings starting with A and continuing with some
permutation of BCD.

We are now faced with the problem of finding all the strings containing BCD
(which we can simply append to A to get the full permutation).

We can think of the permutations of this string as being divided into three
sets (because the string is three characters long):

1) All the strings starting with B and continuing with some permutation of
CD
2) All the strings starting with C and continuing with some permutation of
DB
3) All the strings starting with D and continuing with some permutation of
BC

As you might imagine, you can get at these sets simply by moving the string
around a little.

Let's look at just one of these sets (because the others are dealt with in
the same way, obviously): strings starting with (A and then) B and
continuing with some permutation of CD.

We are now faced with the problem of finding all the strings containing CD
(which we can simply append to AB to get the full permutation).

We can think of the permutations of this string as being divided into two
sets (because the string is two characters long):

1) All the strings starting with C and continuing with some permutation of D
2) All the strings starting with D and continuing with some permutation of C

As you might imagine, you can get at these sets simply by moving the string
around a little.

Let's look at just one of these sets (because the others are dealt with in
the same way, obviously): strings starting with (A and then B and then) C,
and continuing with some permutation of D.

We are now faced with the problem of finding all the strings containing D
(which we can simply append to ABC to get the full permutation).

We can think of the permutations of this string as being divided into one
set (because the string is one character long):

1) All the string starting with D - which is obvious, of course.

The canonical way to do this is via recursion, passing in the string,
together with the number of items yet to be permuted. If that number is 0,
simply display the string. Otherwise, loop around the leftmost
not-yet-handled character and, within the loop, recurse with n-1.

Now take a crack at it yourself, and let us know how you get on.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lew Pitcher  
View profile  
 More options Jul 20 2006, 2:41 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "Lew Pitcher" <lpitc...@sympatico.ca>
Date: 20 Jul 2006 11:41:23 -0700
Local: Thurs, Jul 20 2006 2:41 pm
Subject: Re: print all permutations of string
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

anurag wrote:
> hey can anyone help me in writing a code in c (function) that prints
> all permutations of a string.please help

IIRC, this favour has been requested a number of times in recent weeks.
I wonder why the sudden interest in permuting strings using C
functions.

In any case, to give a concrete example of what R.H. discusses
elsethread, here's an attempt I made a few weeks ago, when the question
first came up. Take it as you will.

For the regulars: yes I know that answering a homework question is
frowned apon, and even worse is answering an algorithm question, but
this one piqued my interest. So, for my one freebie a year, I post this
code   ;-)

==snip==

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void rotate(unsigned length, char *string)
{
  char save;

  save = *string;
  while(--length)
  {
    *string=*(string+1);
    ++string;
  }
  *string = save;

}

void permute(unsigned length, char *string, unsigned depth)
{

  if (length == 0)
    printf("%s\n",string-depth);
  else
  {
    unsigned count;

    for (count = length ; count > 0; --count)
    {
      permute(length-1,string+1,depth+1);
      rotate(length,string);
    }
  }

}

int main(int argc, char **argv)
{
  while (--argc)
  {
    char *source = malloc(strlen(*++argv)+1);

    if (source)
    {
      strcpy(source,*argv);
      printf("\nPermuting \"%s\"\n",source);

      permute(strlen(source),source,0);

      free(source);
    }
  }
  return EXIT_SUCCESS;

}

==snip==

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEv84lagVFX4UWr64RAq3YAKDBs4//FGSrc+zn7+duG2bRtCuRaQCfSnOS
mg6QbOGNExUVVsXBp5lQYD8=
=pYBy
-----END PGP SIGNATURE-----


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Flash Gordon  
View profile  
 More options Jul 20 2006, 2:22 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
Followup-To: alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Flash Gordon <s...@flash-gordon.me.uk>
Date: Thu, 20 Jul 2006 19:22:16 +0100
Local: Thurs, Jul 20 2006 2:22 pm
Subject: Re: print all permutations of string

anurag wrote:
> hey can anyone help me in writing a code in c (function) that prints
> all permutations of a string.please help

Yes. First learn that C++ and C are different languages, so posting to
comp.lang.c++ is entirely inappropriate if you want a C solution.

Having excluded comp.lang.c++ you should first try to write your
algorithm. comp.lang.c does not generally help with algorithms,
comp.programming and alt.comp.lang.c-c++ might, check the groups out to
see. comp.lang.c discusses the C language and helps people with C problems.

Then, post specific questions rather than just asking someone to help
you writing it. A lot of us could write the function in less time that
it would take to explain it to you, but this would not help you to learn.

If you don't know where to start, then I suggest you start off by trying
to write all the permutations of a short string by hand rather than
trying to write a program to do it. Then you can analyse how you did it
and try to formalise that in to an algorithm.

I've excluded comp.lang.c++ from the follow-ups. When you have decided
whether you have a problem with the C language or with writing the
algorithm please cut the posting down to only the most appropriate group.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francis Glassborow  
View profile  
 More options Jul 20 2006, 4:52 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Francis Glassborow <fran...@robinton.demon.co.uk>
Date: Thu, 20 Jul 2006 21:52:06 +0100
Local: Thurs, Jul 20 2006 4:52 pm
Subject: Re: print all permutations of string
In article <1153418276.122549.15...@m79g2000cwm.googlegroups.com>,
anurag <anurag1...@gmail.com> writes

>hey can anyone help me in writing a code in c (function) that prints
>all permutations of a string.please help

That looks like homework. In addition it is under specified. Are the
characters in the string all unique or are repeats allowed? If they are
all unique it is very easy :)

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Jul 20 2006, 5:46 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Keith Thompson <ks...@mib.org>
Date: Thu, 20 Jul 2006 21:46:28 GMT
Local: Thurs, Jul 20 2006 5:46 pm
Subject: Re: print all permutations of string

Francis Glassborow <fran...@robinton.demon.co.uk> writes:
> In article <1153418276.122549.15...@m79g2000cwm.googlegroups.com>,
> anurag <anurag1...@gmail.com> writes
>>hey can anyone help me in writing a code in c (function) that prints
>>all permutations of a string.please help

> That looks like homework. In addition it is under specified. Are the
> characters in the string all unique or are repeats allowed? If they
> are all unique it is very easy :)

Not as easy as if they're all the same!

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Noah Roberts  
View profile  
 More options Jul 20 2006, 6:11 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "Noah Roberts" <roberts.n...@gmail.com>
Date: 20 Jul 2006 15:11:53 -0700
Local: Thurs, Jul 20 2006 6:11 pm
Subject: Re: print all permutations of string

anurag wrote:
> hey can anyone help me in writing a code in c (function) that prints
> all permutations of a string.please help

Knuth, Volume 4 fascicle 2.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alf P. Steinbach  
View profile  
 More options Jul 20 2006, 6:28 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "Alf P. Steinbach" <al...@start.no>
Date: Fri, 21 Jul 2006 00:28:07 +0200
Local: Thurs, Jul 20 2006 6:28 pm
Subject: Re: print all permutations of string
* Noah Roberts:

> anurag wrote:
>> hey can anyone help me in writing a code in c (function) that prints
>> all permutations of a string.please help

> Knuth, Volume 4 fascicle 2.

As I recall, Knuth does not discuss or mention arithmetic coding of
permutations (using the factorial number system), so is not a complete
reference.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Pfaff  
View profile  
 More options Jul 20 2006, 6:31 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Ben Pfaff <b...@cs.stanford.edu>
Date: Thu, 20 Jul 2006 15:31:59 -0700
Local: Thurs, Jul 20 2006 6:31 pm
Subject: Re: print all permutations of string
"Alf P. Steinbach" <al...@start.no> writes:

> * Noah Roberts:
>> anurag wrote:
>>> hey can anyone help me in writing a code in c (function) that prints
>>> all permutations of a string.please help
>> Knuth, Volume 4 fascicle 2.

> As I recall, Knuth does not discuss or mention arithmetic coding of
> permutations (using the factorial number system), so is not a complete
> reference.

Is that necessary to answer the OP's question?  I doubt it.
--
"doe not call up Any that you can not put downe."
--H. P. Lovecraft

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Brody  
View profile  
 More options Jul 20 2006, 8:09 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Kenneth Brody <kenbrody+clc20060...@spamcop.net>
Date: Thu, 20 Jul 2006 20:09:29 -0400
Local: Thurs, Jul 20 2006 8:09 pm
Subject: Re: print all permutations of string

Lew Pitcher wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1

> anurag wrote:
> > hey can anyone help me in writing a code in c (function) that prints
> > all permutations of a string.please help

> IIRC, this favour has been requested a number of times in recent weeks.
> I wonder why the sudden interest in permuting strings using C
> functions.

Perhaps from summer school assignments from all those students who
failed their C classes?

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody        | www.hvcomputer.com | #include              |
| kenbrody/at\spamcop.net | www.fptech.com     |    <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamT...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Noah Roberts  
View profile  
 More options Jul 21 2006, 11:04 am
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "Noah Roberts" <roberts.n...@gmail.com>
Date: 21 Jul 2006 08:04:08 -0700
Local: Fri, Jul 21 2006 11:04 am
Subject: Re: print all permutations of string

Alf P. Steinbach wrote:
> * Noah Roberts:
> > anurag wrote:
> >> hey can anyone help me in writing a code in c (function) that prints
> >> all permutations of a string.please help

> > Knuth, Volume 4 fascicle 2.

> As I recall, Knuth does not discuss or mention arithmetic coding of
> permutations (using the factorial number system), so is not a complete
> reference.

Yeah, I don't know what's actually in it.  I have it but haven't gotten
the time to read it yet.  Knuth takes me an extraordinary amount of
time to read.  But it's title is "Generating all permutations and
combinations" so I figured it would be appropriate.  Guy might learn
something instead of having his homework done for him.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Biber  
View profile  
 More options Jul 21 2006, 11:25 am
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Simon Biber <n...@ralmin.cc>
Date: Fri, 21 Jul 2006 15:25:50 GMT
Local: Fri, Jul 21 2006 11:25 am
Subject: Re: print all permutations of string

Alf P. Steinbach wrote:
> * Noah Roberts:
>> anurag wrote:
>>> hey can anyone help me in writing a code in c (function) that prints
>>> all permutations of a string.please help

>> Knuth, Volume 4 fascicle 2.

> As I recall, Knuth does not discuss or mention arithmetic coding of
> permutations (using the factorial number system), so is not a complete
> reference.

When you refer to arithmetic coding of permutations using the factorial
number system, are you talking about an algorithm that assigns a unique
index number to each permutation and can return any individual
permutation given its index number?

I have implemented a system like that below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned long long ull;

/* calculate factorial of given number */
ull fact(ull a)
{
   ull result = 1;
   while(a) result *= a--;
   return result;

}

/* generates a permutation of str and stores it in 'rs'
    the permutation generated is index number 'no'
    of 'np' total permutations (np == fact(strlen(str)) */
void get_perm(char *rs, const char *str, ull no, ull np)
{
   size_t len = strlen(str);
   ull nm = no;
   ull lt;
   char *p;
   char temp;
   strcpy(rs, str);

   for(p = rs; *p; p++)
   {
     np = np / len;
     lt = nm / np;
     nm = nm % np;

     temp = p[0];
     p[0] = p[lt];
     p[lt] = temp;

     len--;
   }

}

void print_all(const char *str)
{
   size_t len = strlen(str);
   char *rs = malloc(len + 1);
   ull np = fact(len);
   ull i;
   if(!rs)
   {
     fprintf(stderr, "Error allocating memory\n");
   }
   else for(i = 0; i < np; i++)
   {
     get_perm(rs, str, i, np);
     printf("%4lld: %s\n", i, rs);
   }

}

int main(int argc, char **argv)
{
   if(argc == 2)
   {
     print_all(argv[1]);
   }
   else
   {
     fprintf(stderr, "Error: require one argument for"
                     " string to permute\n");
   }
   return 0;


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lovecreatesbeauty  
View profile  
 More options Jul 24 2006, 10:43 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "lovecreatesbeauty" <lovecreatesbea...@gmail.com>
Date: 24 Jul 2006 19:43:50 -0700
Local: Mon, Jul 24 2006 10:43 pm
Subject: Re: print all permutations of string

Lew Pitcher wrote:
> For the regulars: yes I know that answering a homework question is
> frowned apon, and even worse is answering an algorithm question, but
> this one piqued my interest. So, for my one freebie a year, I post this
> code   ;-)

The code gives the same amount of permutations for following two cases,
is it correct?

Permuting "abcde"
abcde
abced
...

Permuting "abcdd"
abcdd
abcdd
...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lovecreatesbeauty  
View profile  
 More options Jul 24 2006, 11:10 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "lovecreatesbeauty" <lovecreatesbea...@gmail.com>
Date: 24 Jul 2006 20:10:47 -0700
Local: Mon, Jul 24 2006 11:10 pm
Subject: Re: print all permutations of string

Why? Up to now, the code given by you C experts are all wrong. Are you
all line-shooters?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Pfaff  
View profile  
 More options Jul 25 2006, 12:13 am
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Ben Pfaff <b...@cs.stanford.edu>
Date: Mon, 24 Jul 2006 21:13:40 -0700
Local: Tues, Jul 25 2006 12:13 am
Subject: Re: print all permutations of string

"lovecreatesbeauty" <lovecreatesbea...@gmail.com> writes:
> Why? Up to now, the code given by you C experts are all wrong. Are you
> all line-shooters?

Some code I wrote for generating permutations can be found in
ll_next_permutation and ll_prev_permutation at
        http://cvs.savannah.gnu.org/viewcvs/pspp/src/libpspp/ll.c?rev=1.1&roo...
It targets linked lists, not strings.

There's a test program at
        http://cvs.savannah.gnu.org/viewcvs/*checkout*/pspp/tests/libpspp/ll-...
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ivanna Pee  
View profile  
 More options Jul 25 2006, 9:21 am
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: "Ivanna Pee" <fr...@Infectedmail.com>
Date: 25 Jul 2006 06:21:48 -0700
Local: Tues, Jul 25 2006 9:21 am
Subject: Re: print all permutations of string

Now try it recursively.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark P  
View profile  
 More options Jul 25 2006, 2:47 pm
Newsgroups: comp.lang.c++, alt.comp.lang.learn.c-c++, comp.lang.c, comp.programming
From: Mark P <use...@fall2005REMOVE.fastmailCAPS.fm>
Date: Tue, 25 Jul 2006 18:47:11 GMT
Local: Tues, Jul 25 2006 2:47 pm
Subject: Re: print all permutations of string

Noah Roberts wrote:
> anurag wrote:
>> hey can anyone help me in writing a code in c (function) that prints
>> all permutations of a string.please help

> Knuth, Volume 4 fascicle 2.

Based on their source I assume these are "legal" links to the pre-fascicles:

http://www.cs.utsa.edu/~wagner/knuth/

-Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »