Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- const int NUMCHARS = 26;
- const int MAXCHOICES = 10;
- char typos[NUMCHARS][MAXCHOICES];
- int choices[NUMCHARS];
- string *words;
- int numWords = 0;
- //return true or false - whether word can be found in words[] array
- bool dictionaryWord(string word) {
- //CODE HERE
- int minimum=0, maximum=numWords-1, half=(minimum+maximum)/2;
- while(minimum<=maximum)
- {
- if (words[half]==word)
- {
- return true;
- }
- else if (words[half]<word)
- {
- minimum=half+1;
- }
- else
- {
- maximum=half-1;
- }
- }
- return false;
- }
- int alternateWords = 0;
- //consider all the alternate characters for current index, recurse for all positions
- void countAlternates(int index, string word) {
- //CODE HERE!
- int charIndex=word[index]-'a';
- for (int i=1; i<=index; i++)
- {
- word[index]=typos[charIndex][i];
- if (index==word.length())
- cout << word;
- }
- }
- int main() {
- ifstream dictionaryFile("words.txt");
- string line;
- //count all dictionary words
- while (dictionaryFile >> line)
- numWords++;
- //read all dictionary words
- words = new string [numWords];
- dictionaryFile.clear();
- dictionaryFile.seekg(0, ios::beg);
- for(int i=0; i<numWords; i++)
- dictionaryFile >> words[i];
- //typo possibilities for each key
- ifstream choicesFile("typo.txt");
- for(int i=0; i<NUMCHARS; i++) {
- getline(choicesFile, line);
- istringstream choiceStr(line);
- int numChoices=0;
- while (choiceStr >> typos[i][numChoices])
- numChoices++;
- choices[i] = numChoices;
- }
- //we need to find all possible alternate words for the input word
- string searchWord;
- cin >> searchWord;
- countAlternates(0, searchWord);
- if (!alternateWords)
- cout << "None";
- }
Advertisement
Add Comment
Please, Sign In to add comment