In: Computer Science
In this assignment you will be implementing a function that will find all single-word anagrams given a single word and a list of all words. In the starter code you will the starting source file anagram.cpp and the list of words wordlist.txt. You will need to implement the anagram() function which takes a string for the word to find anagrams of as the first parameter and a vector of strings containing a list of all words as the second parameter. The return value of anagram() will be a vector of strings containing all anagrams (including the parameter word if it is an actual word). There is a main function that will test the anagram() function as well as an already-implemented function loadWordlist() which will read in words from the specified file with the format by string-type parameter and return these words in a vector of strings. The anagram() function will take a word (string) and a wordlist of all words (vector of strings) and builds a dictionary/map where the key is a specific amount of times each letter occurs in a word and the associated value is a vector of strings containing all words using those letters (anagrams). You may either modify the Stringset class to be a dictionary (holding a key:value pair as opposed to just a key) or use the STL unordered_map structure. The main() function provided will test the anagram() function with a word the user types in using the words stored from wordlist.txt in wordlist, for example if the user types in steak, anagrams=anagram("steak",wordlist); will run and if implemented correctly the following contents of anagrams will be displayed:
skate stake steak takes teaks
Submit anagram.cpp with the implemented anagram() function. You are free to implement any helper functions in anagram.cpp.
Anagram.cpp:
#include
#include
#include
#include
using namespace std;
vector loadWordlist(string filename);
vector anagram(string word, vector wordlist);
int main()
{
vector words;
vector anagrams;
string inputWord;
words=loadWordlist("wordlist.txt");
cout << "Find single-word anagrams for the following word:
";
cin >> inputWord;
anagrams = anagram(inputWord, words);
for (int i=0; i {
cout << anagrams[i] << endl;
}
return 0;
}
vector loadWordlist(string filename)
{
vector words;
ifstream inFile;
string word;
inFile.open(filename);
if(inFile.is_open())
{
while(getline(inFile,word))
{
if(word.length() > 0)
{
words.push_back(word);
}
}
inFile.close();
}
return words;
}
vector anagram(string word, vector wordlist)
{
}
Stringset Class:
class Stringset
{
private:
vector<list<string>> table;
int num_elems;
int size;
public:
Stringset();
vector<list<string>> getTable() const;
int getNumElems() const;
int getSize() const;
void insert(string word);
bool find(string word) const;
void remove(string word);
};
Hello Buddy!
Here is the solution code.
Although, wordlist.txt file was not provided, so I made a simple one for testing the code. Also there are many mistakes and blanks in the provided starting template code. I fixed them all too.
Everything is working fine. Hope it helps!
code is commented in the required part. if you face any issue, feel free to ask.
Anagram.cpp
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
vector<string> loadWordlist(string filename);
vector<string> anagram(string word, vector<string> wordlist);
int main()
{
vector<string> words;
vector<string> anagrams;
string inputWord;
words=loadWordlist("wordlist.txt");
cout << "Find single-word anagrams for the following word: ";
cin >> inputWord;
anagrams = anagram(inputWord,words);
for (int i=0; i<anagrams.size();i++ ){
cout << anagrams[i] << endl;
}
return 0;
}
vector<string> loadWordlist(string filename)
{
vector<string> words;
ifstream inFile;
string word;
inFile.open(filename);
if(inFile.is_open()){
while(getline(inFile,word)){
if(word.length() > 0){
words.push_back(word);
}
}
inFile.close();
}
return words;
}
vector<string> anagram(string word, vector<string> wordlist)
{ //a variable to store one item from wordlist
string item;
// a vector to store all the found anagrams
vector<string> anagrams_vector;
//pushing the original word in anagram_vector
anagrams_vector.push_back(word);
//looping through all the items in wordlist
for(int k=0;k<wordlist.size();k++){
//take out one item from wordlist
item = wordlist[k];
int l1 = word.length();
int l2 = item.length();
//initializing unordered map , so that we can match the number of characters in both strings
unordered_map<char,int> m;
if(l1!=l2){
//if different lenght, they can't be anagram
continue;
}
for(int i=0;i<l1;i++){
//increasing key-values for those characters, which are in word(user input)
m[word[i]]++;
}
for(int i=0;i<l2;i++){
if(m.find(item[i])==m.end()){
//if any character, that was in word, not found in the item, then they are not anagram
break;
}
else{
//decreasing the corresponding values of no. of characters, which are found in item
m[item[i]]--;
if(m[item[i]]==0){
//if all characters will be zero, then whole map will become empty.
m.erase(item[i]);
}
}
}
//if map is empty, it means 'word' and 'item' are anagram.
if(m.size()==0){
anagrams_vector.push_back(item);
}
}
return anagrams_vector;
}
.
wordlist.cpp
nikhil
khilin
niklhi
kkkkkkj
nikhik
jhirkk
inkhil
khkhkh
ikihln
nhikil
kjhkgsdkjfhkj
Std Input : "nikhil"
Output :
Thumbsup ;)