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
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
int CountCharacter(ifstream& Text, char CompareCharacter);
string ReadLine(ifstream& Text);
bool CompareByCharacter(string Text1, string Text2);
int main()
{
ifstream ListFile("wordlist.txt");//Load the word list
if (ListFile.is_open())
{//If it loaded successfully
unsigned int CurrentWordCount = CountCharacter(ListFile, '\n');//Count the number of line breaks (aka the number of words)
if (CurrentWordCount == 0)
{//If the number of words is 0
ListFile.close();//Close the list and inform the user.
cout << "\"wordlist.txt\" is empty." << endl;
}
else
{
list<string> WordList;//Create a list for the words
for (unsigned int i = 0; i <= CurrentWordCount; ++i)
{WordList.insert(WordList.end(), ReadLine(ListFile));}//Load all of the words from the document into the list
ListFile.close();//Close the list
//Request and load the word which needs to be unscrambled
string ScrambledWord;
cout << "Enter scrambled word: \n" << endl;
getline(cin, ScrambledWord);
for (list<string>::iterator i = WordList.begin(); i != WordList.end(); i++)
{//For the length of the word list,
if (!CompareByCharacter(*i, ScrambledWord))//Check each word to see if it has the same character composition as the word entered
{WordList.erase(i);}//If it doesn't, erase the word.
else//Otherwise, end the loop immediately.
{break;}
}
if (WordList.size() == 0)//If the word list is empty, inform the user.
{cout << "No match found." << endl;}
else//Otherwise, present the first word of the list.
{cout << WordList.front() << endl;}
}
}
else//Else, notify the user that the file could not be opened
{cout << "Could not open file, \"wordlist.txt\"." << endl;}
return 0;
}
int CountCharacter(ifstream& Text, char CompareCharacter)
{//Count the number of times a character occurs in an ifstream. Takes the ifstream which it searches, and a character to look for.
ifstream::pos_type OriginalPosition = Text.tellg();//Tract the original position so that it may be returned there once done.
Text.seekg(0);//Go to the beginning of the file.
int Count = 0;//Create a count variable which will be returned.
while (Text.good())
{//While we are able to work within the file,
char CurrentCharacter;//create a character and fill it with the next character from the file.
Text.get(CurrentCharacter);
if (CurrentCharacter == CompareCharacter)
{Count++;}//If the character is the same as the one provided, add one to the count and repeat.
}
Text.clear();//Clear the error state (we should have gotten an error after reaching the end of the file).
Text.seekg(OriginalPosition);//Return to the original position.
return Count;//Return the count.
}
string ReadLine(ifstream& Text)
{//Return a line from an ifstream. Takes an ifstream to be read.
string Line;//Create a string which we will return
while(Text.good())
{//While we are able to work with the file,
char CurrentCharacter;//create a character and fill it with the next character from the file.
Text.get(CurrentCharacter);
if (CurrentCharacter != '\n')//If the character isn't a line break, add it to the string.
{Line += CurrentCharacter;}
else//Else, end the loop, jumping strait to the "return" statement
{break;}
}
return Line;//Return the Line
}
bool CompareByCharacter(string Text1, string Text2)
{//Check two strings to see if their character composition is the same. Takes two strings, Text1 and Text2, which are compared.
for (unsigned int i = 0; i < Text1.length(); i++)
{//For the length of the first string,
size_t Position = Text2.find_first_of(Text1[i]);//find the position of each character of the first string in the second string.
if (Position == string::npos)
{return false;}//If the character isn't there, they aren't the same.
else//If it is, remove it. This prevents a character from being counted twice so that this doesn't happen: CompareByCharacter("clock", "lock") == true
{Text2.replace(Position, 1, "");}
}
return true;//Return true since all the characters were found
}
| |