Question
C++ Code: Start with the startC.cpp code provided below along with oed.txt. Read a single word in main() and find all anagrams using all the
C++ Code:
Start with the startC.cpp code provided below along with oed.txt. Read a single word in main() and find all anagrams using all the letters once. An anagram is rearranging the letters to make a valid English word. For example hoolsc can be re-arranged to spell school by moving the last two letters to the beginning of the word.
If a word has multiple of the same letter, like reet, you can spell the rearranged word multiple times. So for this example, you can spell tree two ways: (1) by moving the t to the start and (2) by swapping the middle two es, then moving the t to the start. Or in other words, if you start with: re1e2t you can make tree by: tre1e2 or tre2e1. Note: don't run this for a word that has more than 5 characters. It will take too long to finish.
You must use recursion for this problem. You may not use any loops (or goto) to solve this.
-------------------------------------------------------------------------------------------------------------------------------------------
startC.cpp
#include#include #include #include using namespace std; bool first=true; int dlen=0; string dicWords[40000]; bool isWord(string s); string toLower(string a); int caseInsensitiveStringCompare(const string& str1, const string& str2); int main() { return 0; } bool isWord(string s) { if(first) { int i=0; first=false; ifstream dic; dic.open("oed.txt"); if(dic.fail()) { cout << "file \"oed.txt\" not in current directory "; return false; } string name, desc; while(dic >> name) { getline(dic, desc); if(desc == "") { continue; } if(desc.find(" ") != string::npos) { string firstWord = desc.substr(0, desc.find("")); if(firstWord == "prefix" || firstWord == "abbr." || firstWord == "suffix") { continue; } } dicWords[i] = name; i++; } dlen=i; } return inDic(dicWords, 0, dlen-1, s); } bool inDic(string dicWords[], int start, int end, string s) { int mp = (start+end)/2; string mid = dicWords[mp]; if(toLower(mid) == toLower(s) || toLower(dicWords[start]) == toLower(s) || toLower(dicWords[end]) == toLower(s)) { return true; } if( abs(end-start) <= 1) { return false; } if(caseInsensitiveStringCompare(s , mid) < 0) { return inDic(dicWords, start, mp, s); } else { return inDic(dicWords, mp, end, s); } } string toLower(string a) { string result=""; for(unsigned i=0; i < a.length(); i++) { char c = a[i]; result += tolower(c); } return result; } int caseInsensitiveStringCompare(const string& str1, const string& str2) { for (int i=0; i < static_cast (min(str1.length(), str2.length())); i++) { if (tolower(str1[i]) < tolower(str2[i])) { return -1; } if (tolower(str1[i]) > tolower(str2[i])) { return 1; } } if(str1.length() < str2.length()) { return -1; } if(str1.length() > str2.length()) { return 1; } return 0; }
--------------------------------------------------------------------------------------
oed.txt URL:
http://www-users.cselabs.umn.edu/classes/Spring-2017/csci1113/assignments/oed.txt
---------------------------------------------------------------------------------------
Example 1:
Enter word:
gdo
god
dog
Example 2 :
Enter word:
cta
cat
act
Example 3 (user input is underlined):
Enter word:
lkea
leak
lake
kale
Example 4 (user input is underlined):
Enter word:
reet
tree
tree
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started