# Finding all possible words that can be made out of a 6 letter word

• (2 Pages)
• 1
• 2

## 18 Replies - 2706 Views - Last Post: 20 March 2013 - 12:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=315906&amp;s=25a99592b9e679d42613eb6306c84730&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Finding all possible words that can be made out of a 6 letter word

Posted 19 March 2013 - 09:35 PM

The OP can't use that. Post #5.

### #17 Skydiver

• Code herder

Reputation: 4768
• Posts: 15,751
• Joined: 05-May 12

## Re: Finding all possible words that can be made out of a 6 letter word

Posted 19 March 2013 - 10:06 PM

I missed that... looks like he'll have to choose a permutation generation algorithm and implement it. I tend to prefer the recursive permutation generators even though there are more efficient ways to do things.

### #18 Skydiver

• Code herder

Reputation: 4768
• Posts: 15,751
• Joined: 05-May 12

## Re: Finding all possible words that can be made out of a 6 letter word

Posted 19 March 2013 - 10:14 PM

To me there is a certain elegance in the code structured:
```permute(set, depth, slots[])
{
if (set is empty)
save permutation found in slots

for each(item in set)
{
slots[depth] = item
remove item from set
permute(set, depth + 1, slots)
put slots[depth] back in set
}
}

```

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Finding all possible words that can be made out of a 6 letter word

Posted 20 March 2013 - 12:34 AM

I started off working with permutations, but it became too complex - time for a bit o' the Irish Brute (force). />

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

#define MAXWRD 6327  //large word list -"source" to find the targets within
#define MINWRD 8     //small word list - "targets" to find
#define WRDLEN 8    //one more than your longest word length

int main(void) {

char *pchr=NULL;
char A[MAXWRD][WRDLEN];

//the target words
char B[MINWRD][WRDLEN]={"alpha","bravo","charlie","delta","echo","foxtrot","golf","hotel"};
char myWord[WRDLEN];

int a,b,i,k,lenA,lenB,wrdNum;
unsigned long long jumble=0;

FILE *fp=fopen("myWords.txt","r"); //the source file
if(!fp) {
printf("Error opening words file!\n");
return 0;
}
i=0;
while(fscanf(fp, "%s",A[i])>0) {
++i;
}
wrdNum=i;
fclose(fp);

for(b=0;b<MINWRD;b++) {            //for every word in B[]
for(a=0;a<wrdNum;a++) {    //check every word in A[]
lenA=strlen(A[a]);
lenB=strlen(B[b]);
strcpy(myWord,B[b]);    //myWord gets marked up,
if(lenA<=lenB) {        //so it needs a fresh one.
for(k=0;k<lenA && k<lenB;k++) {
if((pchr=strchr(myWord,A[a][k]))==NULL)  //got a match?
break;         //if not, leave
else
*pchr='*';      //else mark it
}
if(k==lenA) {        //all letters in A word, match up to one
//or more letters in B word?
printf("%30s  in  %-8s \n",A[a],B[b]);
++jumble;
}
}
}
}

printf("\nNumber of jumbles found: %llu \n",jumble);
return 0;
}

```

Only a most rudimentary testing made of the above - you're running it at your own risk, of course.

This post has been edited by Adak: 20 March 2013 - 12:35 AM

• (2 Pages)
• 1
• 2

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}