all arrangement of a word

Discussion in 'C Programming' started by longshan, Jul 7, 2008.

  1. longshan

    longshan Guest

    Hi friends,
    Can you help me write some code to list all the arrangement of a word?
    for example
    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?
    longshan, Jul 7, 2008
    1. Advertisements

    Peter Nilsson, Jul 7, 2008
    1. Advertisements

  3. 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, Jul 7, 2008
  4. longshan

    longshan Guest

    longshan, Jul 7, 2008
  5. longshan

    longshan Guest

    I just need some idea about how to do this.
    longshan, Jul 7, 2008
  6. Yes, that is a use of trigraphs, which have been legal in C since C89.

    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.
    Walter Roberson, Jul 7, 2008
  7. longshan

    longshan Guest

    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
    longshan, Jul 7, 2008
  8. longshan

    santosh Guest


    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.

    santosh, Jul 7, 2008
  9. longshan

    CBFalconer Guest

    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';
    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) {
    "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 */
    CBFalconer, Jul 7, 2008
  10. Is this a deliberate mystery or an editing error?
    Ben Bacarisse, Jul 7, 2008
  11. longshan

    CBFalconer Guest

    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.
    CBFalconer, Jul 7, 2008
  12. longshan

    temppimaili Guest

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

    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.
    If you are just looking for an working algorithm without jumping in
    the glory details I would suggest you to take a look at

    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..
    temppimaili, Jul 8, 2008
  13. 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!
    Antoninus Twink, Jul 8, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.