here is my c++ problem I'm working on. I have to use recursion to find all possible anagrams of a word. for example  Please enter a string for an anagram: art Matching word art Matching word rat Matching word tar Please enter a string for an anagram: regardless No matches found the txt file consists of words from A to Z about 28,000 words  need help finishing this problem. but  may not use any of C++'s iteration constructs (do, while, for, and goto) or any STL algorithms   #include #include #include #include using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int loadWords(istream& dictfile, string dict[]); int recBlends(string word, const string dict[], int size, string results[]); void showResults(const string results[], int size); void printPermutations(string prefix, string rest); void loop(string prefix, string rest, int i, int max); 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("words.txt"); if (!dictfile) { cout <<"file not found!" << endl; return (1); } else cout << "file was open\n"; nwords = loadWords(dictfile, dict); cout << "Please enter a string for an anagram: "; cin >> word; int numMatches = recBlends(word, dict, nwords, results); if (!numMatches) cout << "No matches found" << endl; else showResults(results, numMatches); return 0; } int loadWords(istream& dictfile, string dict[]) { string data; if (dictfile.eof()) { // end of dictfile return 0; } dictfile >> data; // uses recursion to loop to the end of dictfile (dict starts with Z) int x = loadWords(dictfile, dict); if (x < MAXDICTWORDS) { dict[x] = data; x++; return x; } else return MAXDICTWORDS; //reached max capacity of array } void showResults(const string results[], int size) { if (size - 1 < 0) return; cout << "Matching word " << results[size - 1] << endl; showResults(results, size - 1); } int recBlends(string word, const string dict[], int size, string results[]) { int back = word.size() - 1; int matched = 0; //permute(word, 0, back, dict, size, matched, results); return matched; } void printPermutations(string prefix, string rest) { //if rest is empty, printing prefix if (rest.length() == 0) { cout << prefix << endl; } else { //otherwise passing prefix and rest to loop, loop from //0 to rest.length-1 loop(prefix, rest, 0, rest.length()); } } //loop method implementation, to loop for values between i and max-1 void loop(string prefix, string rest, int i, int max) { //if i exceeds or equals max, returning if (i >= max) { return; } //else, appending char at index i to prefix string updated_prefix = prefix + rest[i]; //taking out char at index i from rest to create a string without current char //(this is done by concatenating string before index i with string after index i) string updated_rest = rest.substr(0, i) + rest.substr(i + 1); //calling printPermutations, passing these updated values printPermutations(updated_prefix, updated_rest); //moving to the next iteration of the loop, passing i+1 as new value for i loop(prefix, rest, i

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

here is my c++ problem I'm working on. I have to use recursion to find all possible anagrams of a word. for example 

Please enter a string for an anagram: art
Matching word art
Matching word rat
Matching word tar
Please enter a string for an anagram: regardless
No matches found

the txt file consists of words from A to Z about 28,000 words 

need help finishing this problem. but  may not use any of C++'s iteration constructs (do, while, for, and goto) or any STL
algorithms

 

#include <iostream>
#include <fstream>
#include <istream>
#include <cstring>
using namespace std;

const int MAXRESULTS = 20; // Max matches that can be found
const int MAXDICTWORDS = 30000; // Max words that can be read in

int loadWords(istream& dictfile, string dict[]);
int recBlends(string word, const string dict[], int size, string
results[]);
void showResults(const string results[], int size);

void printPermutations(string prefix, string rest);

void loop(string prefix, string rest, int i, int max);


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("words.txt");
if (!dictfile) {
cout <<"file not found!" << endl;
return (1);
}
else
cout << "file was open\n";
nwords = loadWords(dictfile, dict);

cout << "Please enter a string for an anagram: ";
cin >> word;

int numMatches = recBlends(word, dict, nwords, results);
if (!numMatches)
cout << "No matches found" << endl;
else
showResults(results, numMatches);

return 0;
}

int loadWords(istream& dictfile, string dict[]) {
string data;

if (dictfile.eof()) { // end of dictfile
return 0;
}

dictfile >> data;
// uses recursion to loop to the end of dictfile (dict starts with Z)
int x = loadWords(dictfile, dict);
if (x < MAXDICTWORDS) {
dict[x] = data;
x++;
return x;
}
else
return MAXDICTWORDS; //reached max capacity of array
}

void showResults(const string results[], int size) {
if (size - 1 < 0)
return;
cout << "Matching word " << results[size - 1] << endl;
showResults(results, size - 1);

}

int recBlends(string word,
const string dict[], int size, string results[])
{
int back = word.size() - 1;
int matched = 0;
//permute(word, 0, back, dict, size, matched, results);

return matched;
}

void printPermutations(string prefix, string rest) {

//if rest is empty, printing prefix

if (rest.length() == 0) {

cout << prefix << endl;

}
else {

//otherwise passing prefix and rest to loop, loop from

//0 to rest.length-1

loop(prefix, rest, 0, rest.length());

}

}

//loop method implementation, to loop for values between i and max-1

void loop(string prefix, string rest, int i, int max) {

//if i exceeds or equals max, returning

if (i >= max) {

return;

}

//else, appending char at index i to prefix

string updated_prefix = prefix + rest[i];

//taking out char at index i from rest to create a string without current char

//(this is done by concatenating string before index i with string after index i)

string updated_rest = rest.substr(0, i) + rest.substr(i + 1);

//calling printPermutations, passing these updated values

printPermutations(updated_prefix, updated_rest);

//moving to the next iteration of the loop, passing i+1 as new value for i

loop(prefix, rest, i + 1, max);

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education