I have a word list and a file containing a number of anagrams. These anagrams are words found in the word list. I need to develop an algorithm to find the matching words and produce them in an output file. The code I have developed so far has only worked for the first two words. In addition, I can't get the code to play nice with strings containing numbers anywhere in it. Please tell me how I can fix the code.
EDIT: Oops, I left the x and y uninitialized, causing the code to crashCode:#include <iostream> #include <fstream> #include <string> using namespace std; int main (void) { int x = 0, y = 0; int a = 0, b = 0; int emptyx, emptyy; int match = 0; ifstream f1, f2; ofstream f3; string line, line1[1500], line2[50]; size_t found; f1.open ("wordlist.txt", ios::in); f2.open ("file.txt", ios::in); f3.open ("output.txt", ios::out); //stores content into string arrays if (f1.is_open() && f2.is_open()) { while (f1.eof() == 0) { getline (f1, line); line1[x] = line; x++; } while (f2.eof() == 0) { getline (f2, line); line2[y] = line; y++; } //finds position of last elements emptyx = x-1; emptyy = y-1; //matching algorithm for (y = 0; y <= emptyy; y++) { for (x = 0; x <= emptyx; x++) { if (line2[y].length() == line1[x].length()) { for (a = 0; a < line1[x].length(); a++) { found = line2[y].find(line1[x][a]); if (found != string::npos) { match++; line2[y].replace(found, 1, 1, '.'); if (match == line1[x].length()) { f3 << line1[x] << ", "; match = 0; } } } } } } f1.close(); f2.close(); f3.close(); } //file access error else cout << "Input error."; return 0; }



LinkBack URL
About LinkBacks


