Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! So I wrote a function that is supposed to make anagrams of words. So, if I e

ID: 3857173 • Letter: H

Question

Hi! So I wrote a function that is supposed to make anagrams of words. So, if I enter top, it should give me top, otp, pot, opt, pto.

This is what I wrote

void printPermutations(string prefix, string rest)

{

if (rest == "")

cout << prefix << endl;

else{

int i=0;

if (i>=rest.size())

return;

prefix += rest[i];

string newrest;

newrest = rest.erase(0, 1);

rest = newrest;

printPermutations(prefix, rest);

}

}

but if I implement it, it will only print out "top" which is what you type in. Here is my main function. What is wrong with it?

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;

string n[MAXDICTWORDS];

dictfile.open("/Users/Sadafshar/Desktop/words.txt");

//cout<<readDictionary(dictfile, n);

printPermutations("", "bird");

if (!dictfile) {

cout << "File not found!" << endl;

return (1);

}

  

}

Explanation / Answer

Hi

Please upload your sample text file.which you used to read.because,i try error your code.and something is something

here by i have written how to swap the words.kindy check it

    #include<iostream>
    using namespace std;
    void swap(char& x, char& y)
    {
       char temp;
       temp = x;
       x = y;
       y = temp;
    }
    void permutation(string s,int i,int k)
    {
        int j;
        if (i == k)
        cout << s << " ";
        else
        {
        for (j = i; j < s.length(); j++)
        {
        swap(s[i],s[j]);
        permutation(s, i + 1, k);
        swap(s[i],s[j]);
        }
        }
    }
    int main()
    {
        string s;
        cout << "Enter the string : ";
        cin >> s;
        cout << endl << "The permutations of the given string are : " << endl;
        permutation(s, 0, s.length() - 1);
        cout << endl;

    }

O/P

Enter the string : top                                                                                                                                                                                                                                                                                                                      

The permutations of the given string :                                                                                                                                   

top     tpo     otp     opt     pot     pto  

Once you posted the text file i will read write a code to read from file and then swap for number of combinations

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote