Permutation generator

Scott Anderson scotta at hpcuhd.HP.COM
Tue Oct 16 04:55:49 AEST 1990


    Here's one I did about 2 years ago for solving jumbles (in a pretty
gross way - see leading comment in program).  It doesn't use malloc and
uses pointers instead of indices.  The only minor disadvantage is that
dupes aren't eliminated.  I find it somewhat amusing how similar the
programs are though.

    Scott Anderson
    An RTEsian and proud of it...		Hewlett-Packard
						Data Systems Operation
    scotta at cup.hp.com				11000 Wolfe Rd.  MS 42UN
    408-447-5219				Cupertino, CA  95014
________________________________________________________________________

/* This program produces permutations of all of the strings passed to it.
 * To solve jumbles try:
 *	perm xxxxx | sort -u | tee temp | spell | sort | comm -13 - temp
 * where xxxxx is the scrambled word.
 */

#define SWAP(a, b)	ch = *(a); *(a) = *(b); *(b) = ch;

void perm(whole, partial)
    char *whole, *partial;
{
    register char *ptr, *next_partial, ch;

    if (!*partial) {
	puts(whole);
	return;
    }

    ptr = partial;
    next_partial = partial + 1;
    while (*ptr) {
	SWAP(partial, ptr);
	perm(whole, next_partial);
	SWAP(partial, ptr);
	ptr++;
    }
}

main (argc, argv)
    int argc;
    char **argv;
{
    int i;

    for (i=1; i<argc; i++)
	perm(argv[i], argv[i]);
}



More information about the Alt.sources mailing list