473,796 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 20 '06 #1
20 41308
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
Jul 20 '06 #2
[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)
Jul 20 '06 #3
-----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=*(strin g+1);
++string;
}
*string = save;
}

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

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

for (count = length ; count 0; --count)
{
permute(length-1,string+1,dept h+1);
rotate(length,s tring);
}
}

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

if (source)
{
strcpy(source,* argv);
printf("\nPermu ting \"%s\"\n",sourc e);

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

iD8DBQFEv84lagV FX4UWr64RAq3YAK DBs4//FGSrc+zn7+duG2b RtCuRaQCfSnOS
mg6QbOGNExUVVsX Bp5lQYD8=
=pYBy
-----END PGP SIGNATURE-----

Jul 20 '06 #4
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.programmin g 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
Jul 20 '06 #5
Ico
In comp.lang.c Richard Heathfield <in*****@invali d.invalidwrote:
[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 ...
[ ...snipped yet another lengthy explanation... ]
... 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.
I admire your patience, I really do...

--
:wq
^X^Cy^K^X^C^C^C ^C
Jul 20 '06 #6
In article <11************ *********@m79g2 000cwm.googlegr oups.com>,
anurag <an********@gma il.comwrites
>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
Jul 20 '06 #7
Francis Glassborow <fr*****@robint on.demon.co.ukw rites:
In article <11************ *********@m79g2 000cwm.googlegr oups.com>,
anurag <an********@gma il.comwrites
>>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_Keit h) 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.
Jul 20 '06 #8

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.

Jul 20 '06 #9
* 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?
Jul 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5926
by: Girish Sahani | last post by:
Hi guys, I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) def permute(string): l= l.append(string) string1 = '' for i in range(0,len(string)-1,1): string1 = string + string
17
4709
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
1
10933
by: JosAH | last post by:
Greetings, last week we talked a bit about generating permutations and I told you that this week will be about combinations. Not true; there's a bit more to tell about permutations and that's what this Tip Of The Week is about. Maybe later we'll talk a bit about combinations (if anyone's interested). The little utility class we implemented last week was able to generate a next permutation (if any) given a current permutation. But what...
5
1764
by: Shraddha | last post by:
If we have three variables a,b,c...which are char variables....then hoe to print there all permutations? for example....abc, bac, cab.......all of them....
5
2696
by: Shraddha | last post by:
Suppose we are having 3 variables...a,b,c And we want to print the permutations of these variables...Such as...abc,acb,bca...all 6 of them... But we are not supposed to do it mannually... I want to know that formula by which this can be possible... Then that program will be ok for nnumber of variables.... Can anyone help me for that?
2
5900
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the dictionary essentially creates the following array: Key Value
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.