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

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2019 Views - Last Post: 20 March 2013 - 12:34 AM Rate Topic: -----

#16 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • 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.
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • 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
    }
}


Was This Post Helpful? 0
  • +
  • -

#19 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2