#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <cstring>
#include <assert.h>
using namespace std;
const int MAXRESULTS = 20; // Max matches that can be found
const int MAXDICTWORDS = 30000; // Max words that can be read in
// helper function for vocabularyCreator()
int vocabCreate(istream &dictfile, string dict[], int& wordcount)
{
if (wordcount >= MAXDICTWORDS) //if number of words larger/equal to maxdictwords
{
return wordcount;
}
else if ((dictfile >> dict[0]).eof()) //if end of dictfile has been reached
{
return wordcount;
}
return vocabCreate(dictfile, dict + 1, ++wordcount);
}
//puts each string in dictfile into the array dict
//returns the number of words read into dict (should not be larger than maxdictwords since that is the size of the array
int vocabularyCreator(istream &dictfile, string dict[])
{
int wordcount = 0;
return vocabCreate(dictfile, dict, wordcount);
}
//// -----------------------------------------------------------------------------------------------------------------------------------------------
// function declarations for helper functions
void createAnagrams(string word, int prefixsize, int wordsize, const string dict[], int dictsize, string anagrams[], int& anagramcounter);
void permutateWord(int prefixsize, int wordsize, int n, string word, const string dict[], int dictsize, string anagrams[], int& anagramcounter);
void searchDict(int initial, int max, string anagrams[], const string dict[], int dictsize, string results[], int& resultscounter);
void swap (char& a, char& b);
bool checkForRepeat(string word, const string results[], int resultssize);
void swap(char& a, char& b) //to swap two characters
{
char temp;
temp = a;
a = b;
b = temp;
}
void createAnagrams(string word, int prefixsize, int wordsize, const string dict[], int dictsize, string anagrams[], int& anagramcounter)
{
if (prefixsize == wordsize) // permutation is complete
{
anagrams[anagramcounter] = word; // add word to list of anagrams
anagramcounter++;
return;
}
else
{
permutateWord(prefixsize, wordsize, prefixsize, word, dict, dictsize, anagrams, anagramcounter);
}
}
void permutateWord(int prefixsize, int wordsize, int n, string word, const string dict[], int dictsize, string anagrams[], int& anagramcounter)
{
if (prefixsize >= wordsize)
{
return;
}
swap(word[n], word[prefixsize]);
createAnagrams(word, n+1, wordsize, dict, dictsize, anagrams, anagramcounter);
swap(word[n], word[prefixsize]);
permutateWord(prefixsize+1, wordsize, n, word, dict, dictsize, anagrams, anagramcounter);
}
bool checkForRepeat(string word, const string results[], int resultssize)
{
if (resultssize == 0)
{
return true;
}
else if (word == results[resultssize-1]) // word is found already in results array
{
return false;
}
return checkForRepeat(word, results, resultssize - 1);
}
void searchForAMatch(string word, const string dict[], int dictsize, string results[], int& resultscounter)
{
if (dictsize == 0) //dict is empty, no matches possible
{
return;
}
else if (word == dict[dictsize-1]) // if word matches word in dict
{
if (resultscounter == MAXRESULTS) // matches cannot exceed MAXRESULTS
{
return;
}
if (checkForRepeat(word, results, sizeof(results)) == false) // check if word has already been added to results
{
return;
}
results[resultscounter] = word; // add word if unique
resultscounter++;
return;
}
else
{
searchForAMatch(word, dict, dictsize - 1, results, resultscounter);
}
}
void searchDict(int initial, int max, string anagrams[], const string dict[], int dictsize, string results[], int& resultscounter)
{
if (initial >= max)
{
return;
}
else
{
// if match is possible (dict is not empty), search in dict
searchForAMatch(anagrams[initial], dict, dictsize, results, resultscounter);
}
searchDict(initial+1, max, anagrams, dict, dictsize, results, resultscounter);
}
int potentialSequences(string word, const string dict[], int size, string results[])
{
int numanagrams = 0;
int numresults = 0;
string anagrams[MAXDICTWORDS];
// creates all possible anagrams of word and stores into anagrams array
createAnagrams(word, 0, word.size(), dict, size, anagrams, numanagrams);
// searches each permutation in dict to find matches, matches stored in results array
int initialmatches = 0;
searchDict(initialmatches, numanagrams, anagrams, dict, size, results, numresults);
return numresults;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
//Displays size number of strings from results
void outcomeDisclosure(const string results[], int size)
{
if (size <= 0)
{
return;
}
else
{
cout << results[0] << endl;
outcomeDisclosure(results + 1, size - 1);
}
}
// -------------------------------------------------------------------------------------------------------------------------------------------------
int main()
{
string results[MAXRESULTS];
string dict[MAXDICTWORDS];
ifstream dictfile; // file containing the list of words
int nwords; // number of words read from dictionary
string word;
dictfile.open("/Users/rachelyu/Downloads/words.txt");
if (!dictfile) {
cout << "File not found!" << endl;
return (1);
}
nwords = vocabularyCreator(dictfile, dict);
//cout << nwords << endl;
cout << "Please enter a string for an anagram: ";
cin >> word;
int numMatches = potentialSequences(word, dict, nwords, results);
if (!numMatches)
cout << "No matches found" << endl;
else
outcomeDisclosure(results, numMatches);
}
The code is meant to find all anagrams of a word in an imported dictionary file (excluding repeats) utilizing recursion.
int vocabularyCreator(istream &dictfile, string dict[]);
Puts each string in dictfile into the array dict. Returns the number of words read into dict. This number should not be larger than MAXDICTWORDS since that is the size of the array.
int potentialSequences(string word, const string dict[], int size, string results[]);
Puts all the possibilities of word which are found in dict into results. Returns the number of matched words found. This number should not be larger than MAXRESULTS since that is the size of the array. The size is the number of words inside the dict array.
void outcomeDisclosure(const string results[], int size);
Displays size number of strings from results. The results can be printed in any order.