The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Find all the permutations of rearranging a string
Discuss Find all the permutations of rearranging a string in the C Programming forum on Dev Shed. Find all the permutations of rearranging a string C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 9th, 2013, 11:13 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
Find all the permutations of rearranging a string
I want a C Program to get all the combinations of rearranging a array. Like if i enter "abcd".
I want the program to calculate:
abdc
acdb
acbd
adbc
adcb
bacd
badc
bcda
bcad
bdac
bdca
...............(23 outputs in total)
so that i can check whether any of them satisfies my condition. (through a loop)
Reply ASAP
Thanks in advance
Daksh Shah
|

March 9th, 2013, 11:21 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
what have you done so far?
|

March 10th, 2013, 12:25 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by bullet what have you done so far? |
What do u mean by this????
I had thought of replacing the 1st character by 2nd n then 3rd...
then 2nd with 3rd and so onn..
but it would leave many cases and i want to apply it on a array of length around 100 chars soooo..
pls help
|

March 10th, 2013, 09:45 AM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
Recursion is a simple way to do this.
The permutations of a set of size n is found by removing one element from the set, taking the permutations of the other n-1 elements, and then for each of those permutations, insert the element you removed into each possible spot in the permutation.
|

March 10th, 2013, 09:59 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
Can you please tell me how can this be done; by writing a code in 'c'
|

March 10th, 2013, 02:08 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

March 10th, 2013, 02:51 PM
|
 |
Contributing User
|
|
|
|
|
It might be more efficient to find a string that meets your condition and see if it is a permutation of the input.
__________________
[code] Code tags[/code] are essential for python code!
|

March 10th, 2013, 10:18 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by Daksh What do u mean by this????
I had thought of replacing the 1st character by 2nd n then 3rd...
then 2nd with 3rd and so onn..
but it would leave many cases and i want to apply it on a array of length around 100 chars soooo..
pls help | You might not realize this, but there are 9332621544394415268169923885626670049071596826438162146859296389521759999322991560894146397615651828 6253697920827223758251185210916864000000000000000000000000 permutations of a string of length 100. Good luck waiting many, many, many times longer than the age of the universe while your computer generates those permutations! If you are planning to do this computation for a real problem, then you're going about it the wrong way. Maybe you should give some information about the problem you're really trying to solve. (Just FYI - generating all the permutations of a string is almost never a good way to approach a situation.)
Quote: | Originally Posted by bullet Recursion is a simple way to do this.
The permutations of a set of size n is found by removing one element from the set, taking the permutations of the other n-1 elements, and then for each of those permutations, insert the element you removed into each possible spot in the permutation. | Recursion is definitely the most natural way to do this. Here's a slightly different approach that is easier for me to think about (in C, anyway; yours is perfectly natural in a language like Python that has generators): recursively find all the permutations that have 'a' as the first letter, then the ones that have 'b' as the first letter, and so on up to however many letters you have. If you choose to permute the string in place, assume that each recursive call returns its part of the string to its original unpermuted state, and at the end, you should in turn return the string to its unpermuted state.
|

March 10th, 2013, 10:30 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 22
Time spent in forums: 5 h 28 m 20 sec
Reputation Power: 0
|
|
|
Thanks buddy
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|