1.

Solve : help needed in generating combinations of words?

Answer»

Hi everyone,

I am doing a small assignment of mine.

There is a question to generate all combinations of alphabets a,l,c,h,e,m,i,s,t.

do anyone know how to generate the combinations?

I am doing it in C language.

If anyone knows please help me.

thanks in advance.It is amazing what a Google SEARCH can turn up.

Code: [Select]#include <stdlib.h>
#include <string.h>
#include <stdio.h>
VOID swap(CHAR* src, char* dst)
{
char ch = *dst;
*dst = *src;
*src = ch;
}
/* permute [set[begin], set[end]) */
int permute(char* set, int begin, int end)
{
int i;
int range = end - begin;
if (range == 1) {
printf("set: %s\n", set);
} else {
for(i=0; i<range; i++) {
swap(&set[begin], &set[begin+i]);
permute(set, begin+1, end);
swap(&set[begin], &set[begin+i]); /* set BACK */
}
}
return 0;
}
int main()
{
char str[] = "alchemist";
permute(str, 0, strlen(str));
return 0;
}
Source

"Alchemist" is hardcoded but I'm sure you can tweak the code to prompt for the word. This particular example uses RECURSION but you can also find examples with iteration.



The longer the word, the longer the program will run.thanks sidewinder



Discussion

No Comment Found