Note that there are some explanatory texts on larger screens.

plurals
  1. POAnagram generator for C++ (not using STL)
    primarykey
    data
    text
    <p>I am trying to create an anagram solver just using a very basic, procedural approach. I am finding out that I probably should have done this using classes, but now it is too late and my assignment is about due. Any suggestions on how to figure this out would be great!</p> <p>Basically, this is what the algorithm should do:</p> <ol> <li>Get all words in the dictionary; store them in a container</li> <li>Get a word from the user; quit if appropriate</li> <li>Get all permutations of the word that the user entered</li> <li>Strip the word the user entered from the permutations</li> <li>Strip all words in the permutation collection that aren't also in the dictionary I collected in part 1</li> </ol> <p>Now for the last step, I must make sure that I don't display duplicate anagrams (i.e. anagrams which contain the same letter, such as "loop"). I cannot seem to get this check to work, which is noted below with under the TODO comment block.</p> <p>Any suggestions would be awesome!!</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; // // Change size below to accomodate more anagrams and dictionary words // #define MAX_ANGM_SIZE 4096 #define MAX_WORD_SIZE 1048576 using namespace std; // // Determines whether anagram is valid or not; will not display word // which user entered or words not contained in dictionary // bool isValidAnagram(string word, string userWord, string dictionary[], unsigned int listIdx) { for(unsigned int idx = 0; idx &lt; listIdx; ++idx) { if(word == userWord) return false; else if (word == dictionary[idx]) return true; } return false; } // // Determines whether user's word is contained in the dictionary // or not // bool isValidWord(string word, string dictionary[], unsigned int listIdx) { for(unsigned int idx = 0; idx &lt; listIdx; ++idx) { if(word == dictionary[idx]) return true; } return false; } // // TODO:This function should test for duplicate anagrams and return // true if duplicates are found. // bool isRepeated(string anagrams[], unsigned int anaIdx) { for(unsigned int idx = anaIdx; idx != 0; --idx) { if(anagrams[idx] == anagrams[anaIdx]) return true; else return false; } return false; } // // Only display elements in array which aren't blank and don't // display duplicate anagrams; notify user if no anagrams // were found. // void displayAnagrams(string anagrams[], unsigned int next) { int flag = 0; for (unsigned int idx = 0; idx &lt; next; ++idx) { if((anagrams[idx] != "") || (!(isRepeated(anagrams, idx)))) { if(idx == 1) cout &lt;&lt; " Anagrams: "; if(idx &gt; 0) flag = 1; cout &lt;&lt; anagrams[idx] &lt;&lt; " "; } else continue; } if(flag == 0) cout &lt;&lt; " no anagrams found" &lt;&lt; endl; } static void swap(char &amp;c1, char &amp;c2) { char temp = c1; c1 = c2; c2 = temp; } // // Pass in word to be altered, the userWord for comparison, the array to store // anagrams, the dictionary for comparison, the count for the number of anagrams // and the count for number of dictionary words // static void permute(string word, string userWord, int k, string anagrams[], string dictionary[], unsigned int &amp;next, unsigned int listIdx) { if(k == word.length()-1) { if(isValidAnagram(word, userWord, dictionary, listIdx)) anagrams[next] = word; ++next; } else { for(int idx = k; idx &lt; word.length(); ++idx) { swap(word[k], word[idx]); permute(word, userWord, k+1, anagrams, dictionary, next, listIdx); } } } // // Create container to store anagrams, validate user's word in dictionary, get all // of the anagrams, then display all valid anagrams // void getAnagrams(string word, string dictionary[], unsigned int listIdx) { string anagrams[MAX_ANGM_SIZE]; unsigned int next = 0; if(isValidWord(word, dictionary, listIdx)) { permute(word, word, 0, anagrams, dictionary, next, listIdx); } else { cerr &lt;&lt; " \"" &lt;&lt; word &lt;&lt; "\"" &lt;&lt; " is not a valid word" &lt;&lt; endl; return; } displayAnagrams(anagrams, next); } // // Read in dictionary file, store contents of file in a list, prompt // the user to type in words to generate anagrams // int main() { string file; string word; string quit = "quit"; string dictionary[MAX_WORD_SIZE]; unsigned int idx = 0; cout &lt;&lt; "Enter a dictionary file: "; cin &gt;&gt; file; cout &lt;&lt; "Reading file \"" &lt;&lt; file &lt;&lt; "\"" &lt;&lt; endl; cout &lt;&lt; endl; ifstream inFile(file.c_str()); if(!(inFile.is_open())) { cerr &lt;&lt; "Can't open file \"" &lt;&lt; file &lt;&lt; "\"" &lt;&lt; endl; exit(EXIT_FAILURE); } while(!inFile.eof()) { inFile &gt;&gt; dictionary[idx]; ++idx; } inFile.close(); while(true) { cout &lt;&lt; "Enter a word: "; cin &gt;&gt; word; if(word == quit) break; getAnagrams(word, dictionary, idx); cout &lt;&lt; endl; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload