all arrangement of a word

L

longshan

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
 
K

Keith Thompson

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?
 
L

longshan

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_Keith) (e-mail address removed)  <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.
 
W

Walter Roberson

can any one help me to clarify the code
into order
that contain too many "??"
is that legal form?

Yes, that is a use of trigraphs, which have been legal in C since C89.

I will study the code by my self
but I need the code in code to be readable

http://en.wikipedia.org/wiki/C_trigraph

That will tell you what each ?? sequence corresponds to. Edit
your copy of the source accordingly. Then either use an automatic
code beautifier or a simple text editor to indent the program
into a more readable form.


Having looked at the source code in the posting, I would say that
if your purpose really is just to learn how to understand the code
yourself, that you would be far better off ignoring the code in
that posting and instead sitting down with some paper and a pen
and figuring out how you would write the program, by yourself.

There is little doubt in my mind that the posted code was designed
to be hard to read and understand, likely specifically so that
it could be given to people who asked for code to produce string
permutations, without serious danger that the code would be handed in
as a homework solution, since it should be clear to any marker
that the code was not produced by a student.

It is not uncommon for us to get people claiming that they just need to
produce string permutations as part of something they are doing,
but whom we later find out are looking for a solution to a
homework assignment.

I would go so far as to say that if you continue to indicate that your
purpose in producing permutations is not homework, then it is unlikely
that you will get much (if any) assistance in understanding the code
you were referred to. If you "just want to understand how to do it",
then go ahead and analyze the referenced code on your own --
understanding has no time limit, so there's no problem with
you spending a few months on the task, right? Whereas if you
were at this point to say that Yes, you need this for homework,
then you would lose some points for having previously said otherwis,
but people would become much more open to -teaching- you
(to the extent that you clearly indicated willingness and
ability to learn.)

If you want people to help -teach- you how to create string
permutations, then you should post a summary of what you
have come up with already as to how you would approach the
problem, and you should post -specific- questions about items
you are stuck on. Though if it is the algorithm you are stuck on,
you might get referred to comp.algorithms -- there isn't a whole
lot about producing permutations that is specific to C.
 
L

longshan

Yes, that is a use of trigraphs, which have been legal in C since C89.


http://en.wikipedia.org/wiki/C_trigraph

That will tell you what each ?? sequence corresponds to. Edit
your copy of the source accordingly. Then either use an automatic
code beautifier or a simple text editor to indent the program
into a more readable form.

Having looked at the source code in the posting, I would say that
if your purpose really is just to learn how to understand the code
yourself, that you would be far better off ignoring the code in
that posting and instead sitting down with some paper and a pen
and figuring out how you would write the program, by yourself.

There is little doubt in my mind that the posted code was designed
to be hard to read and understand, likely specifically so that
it could be given to people who asked for code to produce string
permutations, without serious danger that the code would be handed in
as a homework solution, since it should be clear to any marker
that the code was not produced by a student.

It is not uncommon for us to get people claiming that they just need to
produce string permutations as part of something they are doing,
but whom we later find out are looking for a solution to a
homework assignment.

I would go so far as to say that if you continue to indicate that your
purpose in producing permutations is not homework, then it is unlikely
that you will get much (if any) assistance in understanding the code
you were referred to. If you "just want to understand how to do it",
then go ahead and analyze the referenced code on your own --
understanding has no time limit, so there's no problem with
you spending a few months on the task, right? Whereas if you
were at this point to say that Yes, you need this for homework,
then you would lose some points for having previously said otherwis,
but people would become much more open to -teaching- you
(to the extent that you clearly indicated willingness and
ability to learn.)

If you want people to help -teach- you how to create string
permutations, then you should post a summary of what you
have come up with already as to how you would approach the
problem, and you should post -specific- questions about items
you are stuck on. Though if it is the algorithm you are stuck on,
you might get referred to comp.algorithms -- there isn't a whole
lot about producing permutations that is specific to C.

--
  "I buy more from my grocer than he buys from me, and I bet it's
  the same with you and your grocer. That means we have a trade
  deficit with our grocers. Does our perpetual grocer trade deficit
  portend doom?"                              -- Walter Williams

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.
By this chance I can apporoah C closely.
Thanks all u
 
S

santosh

(e-mail address removed) wrote:

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>
 
C

CBFalconer

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>alias jumble
\c\jumble\jumble %1& | sort | uniq | pr -a -T --columns=8

[1] c:\c\jumble>jumble 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;
wd = 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 */
 
C

CBFalconer

Ben said:
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.
 
T

temppimaili

Seems that you got answers already, but I'll add my 2c.

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..
 
A

Antoninus Twink

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.

The attitude of a true professional - this sort of thing really sets you
apart from haxer kids. Above all don't make this sort of magic number
configurable by the user of your program in a command-line option or
config file!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top