Anagram finder

iamthwee 0 Tallied Votes 858 Views Share

Well technically it isn't an anagram finder, more of a word descrambler program.

It is useful for games like scrabble etc.

All you need is a dictionary file, which you can find readily online. Make sure the file has the words on each line and they are all in lowercase.

The program has a good time complexity because it doesn't brute force every permutation of the scrambled letters. That means it can descramble very long words in a short space of time (as long as they are in the dictionary file.)

Notes: Program is NOT thorougly tested, bugs may exist.

Samle output

Enter jumbled set of letters:
rats

***RESULT***

tsar
tars
star
rats
arts
tas
tar
sat
rat
ras
art
ars
ta
at
as
ar
#include <iostream>
//------------------------------ 
// Scrabble program
// Written by Iamthwee 2008(c)
//------------------------------


#include <string>
#include <fstream>
#include <vector>
#include <climits>

using namespace std;

//--------------------------------------------------
//
// Sort the found words by order of word length
// Biggest to smallest
//
//--------------------------------------------------
class Scrabble
{
public:
  string word;
  int length;
};

int Sort ( const Scrabble & left, const Scrabble& right )
{
  if ( left.length > right.length )
  {
    return 1;
  }
  else if ( left.length < right.length )
  {
    return 0;
  }
  else if ( left.length == right.length )
  {
    return -1;
  }
} 
int main()
{
  //read in a dictionary file
  ifstream read ( "c:/dict.txt" );

  int a, b;
  char array[200];
  cout << "Enter jumbled set of letters:" << endl;
  cin >> array; 
  cout << " \n***RESULT***\n" << endl;

  int vm;
  vm = strlen ( array );

  char alphabet[28] = {"-abcdefghijklmnopqrstuvwxyz"};
  int acnt[26];
  int bcnt[26];

  for ( int i = 1; i < 27; i++ )
  {
    acnt[i] = 0;
  }
  for ( a = 0; a < vm; a++ )
  {
    for ( int j = 1; j < 27; j++ )
    {
      if ( array[a] == alphabet[j] )
      {
        acnt[j]++;
      }
    }
  }
  string x;
  vector <Scrabble> stuff;
  while ( read >> x )
  {
    int size;
    size = x.length();
    for ( int i = 1; i < 27; i++ )
    {
      bcnt[i] = 0;
    }
    for ( int i = 0; i < size; i++ )
    {
      for ( int j = 1; j < 27; j++ )
      {
        if ( x[i] == alphabet[j] )
        {
          bcnt[j]++;
        }
      }
    }
    int counter = 0;
    int mx = 0;
    for ( int k = 1; k < 27; k++ )
    {
      if ( bcnt[k] == acnt[k] )
      {
        mx++;
      }
      if ( bcnt[k] <= acnt[k] )
      {
        counter++;
      }
    }
     
    if ( counter == 26 )
    { 
      Scrabble Foo;
      Foo.length = x.length();
      Foo.word = x;

      stuff.push_back ( Foo ); 
    }
  }
  std::sort ( stuff.begin(), stuff.end(), Sort );

  int ac = stuff.size() ;
  
  for ( int i = 0; i < ac ; i++ )
  {
    cout << stuff[i].word << endl;
  }
  read.close(); 
  cin.get();
  cin.get();
  
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.