1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
using namespace std;
class Files {
private:
vector<string>words;
public:
void printAnagram(unordered_map<string, vector<string> >& store);
void storeInMap(vector<string>& vec);
void runner();
void read();
struct Compare {
std::string str;
Compare(const std::string& x) : str(x) {
std::sort(str.begin(), str.end()); std::transform(str.begin(),
str.end(), str.begin(), ::toupper);
}
bool operator ()(const std::string& t)
{
std::string s = t;
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
std::sort(s.begin(), s.end());
return s == str;
}
};
};
void Files::read()
{
string word;
string definition;
string type;
string blank;
int i = 0;
ifstream file("Text.txt");
if (!file) {
cout << "Unable to locate file" << endl;
exit(1);
}
while (file.good())
{
cout << "File loaded successfully." << endl;
cout << "Extracting files. Please Wait....";
do//Gets all the data from the text file and saves it to the vectors
{
getline(file, word, '\n');
words.push_back(word); //pushes to the back of the words vector
i++;
} while (!file.eof());
break;
}
file.close();
}
void Files::runner()
{
string anagram;
cout << "Enter a word to find all the anagrams : ";
cin >> anagram;
string count = copy_if(words.begin(), words.end(), Compare(anagram));
cout << count << endl;
}
int main()
{
Files d;
d.read();
d.runner();
return 0;
}
|