samedi 30 mars 2019

Whilst reading through a datafile, how do I find anagrams of the user's string input?

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.

Aucun commentaire:

Enregistrer un commentaire