473,586 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

all arrangement of a word

Hi friends,
Can you help me write some code to list all the arrangement of a word?
for example
"unix"
it has possibilities like
nixu,ixun,,,and so on.
I think we can get 4!(4*3*2*!)=24 possibilities

But how to list them all by C?
thanks
Jul 7 '08 #1
10 4024
longs...@marchm ail.com wrote:
Hi friends,
Can you help me write some code to list all the arrangement of
a word?
for example
"unix"
it has possibilities like
nixu,ixun,,,and so on.
I think we can get 4!(4*3*2*!)=24 possibilities

But how to list them all by C?
http://groups.google.com/group/comp....c0260bb56e83f2

--
Peter
Jul 7 '08 #2
lo******@marchm ail.com writes:
Hi friends,
Can you help me write some code to list all the arrangement of a word?
for example
"unix"
it has possibilities like
nixu,ixun,,,and so on.
I think we can get 4!(4*3*2*!)=24 possibilities

But how to list them all by C?
thanks
What have you tried? Where did you run into problems? What C
language issues are you having trouble with?

Or did you just want us to do your homework for you?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 7 '08 #3
On Jul 7, 12:25*pm, Keith Thompson <ks...@mib.orgw rote:
longs...@marchm ail.com writes:
Hi friends,
Can you help me write some code to list all the arrangement of a word?
for example
"unix"
it has possibilities like
nixu,ixun,,,and so on.
I think we can get 4!(4*3*2*!)=24 possibilities
But how to list them all by C?
thanks

What have you tried? *Where did you run into problems? *What C
language issues are you having trouble with?

Or did you just want us to do your homework for you?

--
Keith Thompson (The_Other_Keit h) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"
Hi,
I just need some idea about how to do this.
Jul 7 '08 #4
lo******@marchm ail.com wrote:

<snip>
Hi,
It's really not a homework.
Actually I am not even a CS student.
I am a pharmacist.
Nevertheless unless you indicate that you have made an attempt at the
problem (by posting your solution, code or pseudocode, complete or
incomplete, right or wrong), you'll find that almost everyone will
refuse to hand you ready-made code.

<snip>

Jul 7 '08 #5
lo******@marchm ail.com said:

<snip>
Hi,
It's really not a homework.
Actually I am not even a CS student.
I am a pharmacist.
Surely I can spend a few months to figure it out.
But I dont think I have to invent the raw wheel again.
Internet help us to stand on efforts on others
that's why it grow so fast.
As a pharmacist, you will realise that there is more to pharmacy than
asking other people how to make up each specific prescription.

Imagine this situation:

Someone who knows little or nothing about pharmacy opens a shop next door
to yours.
Whenever they get a customer, they bring the prescription round to your
shop.
They don't pay you for your skill in making up the prescription.
They take the medicine back to their own shop, and hand it to the customer
in return for some money.

Do that for very long, and you'll be out of business - and shortly
afterwards, they'll be out of business too, because their source of
information has dried up.

When you studied to become a pharmacist, people were willing to teach you,
yes? But they never said "you don't have to learn anything because we'll
always be there to do it for you".

So - here's a starting point for you:

The number of arrangements of a one-letter word is 1, so working out ALL
the arrangements is easy.

If you have a word with more than one letter in it, say N letters, it's a
little bit harder, because any of the N letters could be at the start. So
take each letter in turn, put it BEFORE the other N-1 letters, and then
solve the problem of calculating the arrangement of N-1 letters.
Eventually, N-1 becomes 1, at which point you can switch to the easy
solution (see above).

Example: STUDY

Let's take each letter in turn and put it at the front:

S TUDY

T UDYS

U DYST

D YSTU

Y STUD

We had a complicated problem - all the arrangements of the five-letter word
STUDY - and we've replaced it with five simpler problems. Each of these
simpler problems hands us a given letter, and a group of only FOUR letters
that need to be arranged. We can solve these sub-problems in the same way.
I'll show you how to do this for the S sub-problem, but the technique is
the same for the other four.

S TUDY

Let's take each letter in turn (from the four whose arrangements we need)
and put it at the front, keeping the S around only so that we don't forget
about it:

(S) T UDY
(S) U DYT
(S) D YTU
(S) Y TUD

We had a complicated problem - all the arrangements of the four-letter word
TUDY - and we've replaced it with four simpler problems. Each of these
simpler problems hands us two given letters (including the S), and a group
of only THREE letters that need to be arranged. We can solve these
sub-problems in the same way. I'll show you how to do this for the T
sub-problem, but the technique is the same for the other three.

(S) T UDY

Let's take each letter in turn (from the three whose arrangements we need)
and put it at the front, keeping the S and T around only so that we don't
forget about them:

(ST) U DY
(ST) D YU
(ST) Y UD

We had a complicated problem - all the arrangements of the three-letter
word UDY - and we've replaced it with three simpler problems. Each of
these simpler problems hands us three given letters (including the S and
T), and a group of only TWO letters that need to be arranged. We can solve
these sub-problems in the same way. I'll show you how to do this for the U
sub-problem, but the technique is the same for the other two.

(ST) U DY

Let's take each letter in turn (from the two whose arrangements we need)
and put it at the front, keeping the S, T and U around only so that we
don't forget about them:

(STU) D Y
(STU) Y D

We had a complicated problem - all the arrangements of the two-letter word
DY - and we've replaced it with three simpler problems. Each of these
simpler problems hands us four given letters (including the S, T and U),
and a group of only ONE letter that needs to be arranged. Since there is
only one way to arrange one letter, this sub-problem is done.

Subjects for further study:

1) arrays
2) counters (to keep track of how many given letters you have)
3) array rotation using copy from front, memmove, copy to back
4) recursion
5) function pointers (so that you know what to do with a final arrangement
once you've found it)

In your testing, remember that N unique items can be arranged in N!
different ways - so 1 item can be arranged in 1 way, 2 items in 1x2=2
ways, 3 items in 1x2x3=6 ways, 4 items in 1x2x3x4=24 ways, and so on. If
your testing gives different numbers, you have a bug. And definitely
remember that 10! is 3628800 (about three and a half million); factorials
grow alarmingly quickly, so don't get all excited about finding all the
arrangements of the word DERMATOGLYPHICS - even if you could find a
million arrangements per second, the 1307674368000 arrangements would take
over two weeks to calculate.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 7 '08 #6
lo******@marchm ail.com wrote:
>
Can you help me write some code to list all the arrangement of a word?
for example
"unix"
it has possibilities like
nixu,ixun,,,and so on.
I think we can get 4!(4*3*2*!)=24 possibilities

But how to list them all by C?
Try the following, combined with the following alias:

[1] c:\c\jumble>ali as jumble
\c\jumble\jumbl e %1& | sort | uniq | pr -a -T --columns=8

[1] c:\c\jumble>jum ble unix
string="unix", max=24, len=4
inux inxu iunx iuxn ixnu ixun niux nixu
nuix nuxi nxiu nxui uinx uixn unix unxi
uxin uxni xinu xiun xniu xnui xuin xuni

and the code:

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

/* Public domain, by C.B. Falconer. *//* 2003-Aug-21 */
/* Attribution appreciated. */

/* Things get out of hand when larger than 8 */
#define MAXWORD 12

/* ------------------ */

/* exchange 0th and ith char in wd */
void trade(char *wd, unsigned int i)
{
char c;

c = *wd;
*wd = wd[i];
wd[i] = c;
} /* trade */

/* ------------------ */

/* Form all n char permutations of the characters in the
string wd of length lgh into outstring at index ix.
Output the results to stdout. */
void jumble(char *wd, unsigned int lgh,
unsigned int ix, /* output place to fill */
unsigned int n, /* max out places to fill */
char *outstring)
{
unsigned int i;

if (0 == n) {
outstring[ix] = '\0';
puts(outstring) ;
}
else
for (i = 0; i < lgh; i++) {
trade(wd, i); /* nop when (0 == i) */
outstring[ix] = *wd;
jumble(wd+1, lgh-1, ix+1, n-1, outstring);
trade(wd, i); /* restore the wd string */
}
} /* jumble */

/* ------------------ */

int main(int argc, char *argv[])
{
unsigned int n, lgh, min;
double max;
char outstring[MAXWORD];

if (argc < 2) {
fprintf(stderr,
"Usage: jumble <baseword[lgh]\n"
" where the (optional) lgh specifies the\n"
" maximum length of the output words\n");
return 0;
}
lgh = strlen(argv[1]);
if (lgh >= MAXWORD) argv[1][lgh = MAXWORD-1] = '\0';

min = lgh;
if ((argc 2) && (1 == sscanf(argv[2], "%u", &n)))
if (n && (n <= lgh)) min = n;

for (n = lgh, max = 1.0; n (lgh - min); n--)
max = max * n;

fprintf(stderr, "string=\"% s\", max=%.0f, len=%u\n",
argv[1], max, min);

jumble(argv[1], lgh, 0, min, outstring);
return 0;
} /* main */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Jul 7 '08 #7
CBFalconer <cb********@yah oo.comwrites:

<snip>
/* Things get out of hand when larger than 8 */
#define MAXWORD 12
Is this a deliberate mystery or an editing error?

--
Ben.
Jul 7 '08 #8
Ben Bacarisse wrote:
CBFalconer <cb********@yah oo.comwrites:

<snip>
>/* Things get out of hand when larger than 8 */
#define MAXWORD 12

Is this a deliberate mystery or an editing error?
As I recall it, the first version ran on a 486, and the runtime
became annoying at 8. Later it was copied to a Pentium system, and
things ran faster, so I allowed that. For my purposes I make it
17, and just abort if I am not willing to wait it out.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Jul 7 '08 #9
Seems that you got answers already, but I'll add my 2c.

On Jul 7, 6:01 am, longs...@marchm ail.com wrote:
Hi friends,
Can you help me write some code to list all the arrangement of a word?
for example
"unix"
it has possibilities like
nixu,ixun,,,and so on.
I think we can get 4!(4*3*2*!)=24 possibilities
For n different character yes, you can have n! different n-length
words as explained previously. But it gets more complicated if not all
the characters differ and in that case the count of possible
permutations decrease.
But how to list them all by C?
thanks
If you are just looking for an working algorithm without jumping in
the glory details I would suggest you to take a look at
http://en.wikipedia.org/wiki/Permutation

All you have to do is to implement the algorithm in C. Now that you
already know the number of possible permutations just pack the
algorithm inside a suitable for-loop and you're done. Note that the
Wikipedia algorithm indexing scheme differs from C..
Jul 8 '08 #10

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

Similar topics

2
1245
by: Darek Adamkiewicz | last post by:
Hello folks I'm new to JavaScript, I wonder if there is something out there JavaScript application which allows to edit/create plan of arrangement e.g. office - I mean to chose element (image) from list and place it (by drag and drop, or another way) in plan area. Such plan when created should be image or html page (on server) easy to view...
8
3503
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word...
8
1997
by: Cuthbert | last post by:
Hi folks, This question is a little deep into memory management of microprocessor. How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top? For example, the "Top-Bottom" is arranging memory resource from higher address to lower address.
4
12426
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
1907
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and PreviousToken(Previous word) and frequency and probability From text file and put each one in cell in table For example if the text file content is "Every man has...
5
2532
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and PreviousToken(Previous word)) from multube files and get frequency of both. it can get for single word but double word give error line 50, in...
2
3748
by: shafwan | last post by:
I want to know more about how to develop a encryption program simple columnar with random arrangement. can you show me how to do it in python because I'm very interested in this language. thanks for your help
6
2835
by: davidcollins001 | last post by:
Hi, I came across a neat problem that I have been trying to code to improve my programming problem solving skills. It is quite simple but I am struggling to get my head around it. Any help would be much appreciated. The problem is the following: a starting word, such as head, is transformed into a second word, such as tail, by way of...
0
1744
by: Pramod Elig | last post by:
I developed this code which works fine in my current system which has office 2007, but when I run in word 2000 this doesn’t work. Can you please help me to code word automation using late binding. I want to create text, tables. Word.Application WApp = new Word.Application(); // Open Microsoft Word application object objMissing...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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...
0
8200
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. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
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...
0
5390
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.