1

Basically I've been given a datafile which contains 100 words and my task is to program an anagram finder to find the anagrams within the datafile. Once the anagram is found, I am struggling to code to print out the word which is in the datafile.

I've managed to sort the strings into alphabetical order to compare and I made an if statement to say if the current word is the same as the original string then print out the word.

I apologise if this question sounds confusing, I've been stuck on this for a few days and I can't wrap my head around this at all.

string FindAnagram(string originalString) {
string currentWord;
string localString;
localString = originalString + currentWord;

ifstream dbFile;
dbFile.open(cDATAFILE);

while(!dbFile.eof()){
    getline(dbFile, currentWord);

      sort (currentWord.begin(), currentWord.end());
      sort (originalString.begin(), originalString.end());

if(currentWord == originalString){
          cout << "\n\t Anagram of the current word: " << localString << endl;
        }
        else {
          cout << "\n\t No anagram available." << endl;
        }

    }
dbFile.close();
return currentWord;
}

For example, if the currentWord is "alert" then it would read through the datafile and print out the words that are an anagram of the word "alert" but I'm struggling to make it print out the word in the datafile.

For example, "later" is expected to be printed out but "alert" is being printed out instead.

Thanks in advance.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Anthony
  • 13
  • 2

1 Answers1

0

You no longer have the word in the datafile, because you mutated it with the sort operation.

Just copy the string before you do that, so you still have the original.

By the way, localstring is weird; why do you append currentWord to it when currentWord is empty?

And you don't need to sort originalString over and over again.

std::string FindAnagram(const std::string& originalString)
{
    std::string originalStringSorted = originalString;
    std::sort(originalStringSorted.begin(), originalStringSorted.end());

    std::ifstream dbFile(cDATAFILE);
    std::string currentWord;
    while (std::getline(dbFile, currentWord))
    {
       std::string currentWordSorted = currentWord;
       std::sort(currentWordSorted.begin(), currentWordSorted.end());

       if (currentWordSorted == originalStringSorted)
       {
          std::cout << "Found '" << currentword << "' to be an anagram of '"
              << originalString << "'\n";
          return currentWord;
       }
    }

    std::cout << "No anagram found\n";
    return "";
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Honestly I was just trying so many things hoping it would work haha but thanks for your answer! EDIT: Do I not need to close the file? (dbFile.close())? – Anthony Mar 30 '19 at 20:13
  • Best to think things through logically. What do you have, and what do you need? What possibilities exist to get from the former to the latter? Programming by guessing doesn't work. Good luck. – Lightness Races in Orbit Mar 30 '19 at 20:15
  • @Anthony No, the file is closed when the `ifstream` goes out of scope. Read https://en.cppreference.com/w/cpp/io/basic_ifstream/close or the relevant chapter of your book – Lightness Races in Orbit Mar 30 '19 at 20:41