The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 6 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
write a program that reads a list of words from a file, outputting only those words that meet certain criteria.
The criterion for this assignment is that folding the word back upon itself yields another valid word. We will interpret folding a word back upon itself as meaning that the new word is formed by
taking the first letter of the original word, followed by last letter, followed by second letter, followed by penultimate letter and so on, until all the letters in the original word are included.
Shown below are some examples.
Â
- cider > cried
- bowl > blowp
- reserve > perverse
- example > eexlapm (Note that eexlapm is not a word and thus should not be included in the output)
Â
So if a word contains 8 characters, the letters for the new words will be taken in the following order:
Â
1 2 3 4 5 6 7 8Â Â Â Â Â Â Â Â >Â Â Â Â Â 1 8 2 7 3 6 4 5
p r e s e r v e              p e r v e r s e
Â
If the word has an odd number of characters, the middle character comes last.
Â
Start with findWords.cpp and findWordsMain.cpp on the bottom of this page and make the following changes:
Â
findWords.cpp
Â
- Â Â Add function string foldWord(string inString) that returns a folded version(as described above) of inString.
- Â Â Add function void lookForFoldedWords(const vector& lexicon) which calls foldWord(string inString) to fold the word and then calls findWord() to
  check for its existence in the lexicon, printing the word only if it exists in the lexicon. The output should consist of the original word, followed by a colon, followed by the folded word (e.g. feigner:freeing).
 Â
findWordsMain.cpp
Â
-Â Â So that we can use different input files ("Enhanced North American Benchmark Lexicon.txt" is a big file and can take a long time to fully process.), we will
  employ a way to specify a different input file at run time." Enhanced North American Benchmark Lexicon.txt" will still be the default if no other file is specified. The way to do this is to declare main as
                         Â
                          int main(int argc, char *argv[])
                         Â
  The parameter argc will contain the number of command-line arguments and argv is an array of c- strings, with the command itself being argv[0], the first command-line argument being argv[1], and so on. Write code so that if argc is more than 1 (that means that there is a command-line argument) the file to open will be argv[1], otherwise the file name will default to" Enhanced North American Benchmark Lexicon.txt".
Â
  There is nothing in our text about command-line arguments, but if you do a web search for argc and argv, you will find lots of information. Here's a pretty good one:  http://web.eecs.utk.edu/~bvz/cs140/Notes/Argv/index.html
 Â
- Â Â Call lookForFoldedWords() to display all the folded words that are themselves words.
Â
Here are both files that need to be change.
Â
findWords.cpp
________________________________________________
#include
#include
#include
#include
#include
Â
using namespace std;
Â
void loadLexicon(vector& lexicon, string fileName) {
  ifstream in;
  string word;
  in.open(fileName.c_str());
  if(in.fail()) {
     cerr << "Cannot open file " << fileName;
     exit(1);
  }
  in >>word;
  while (!in.eof()) {
     lexicon.push_back(word);
     in >> word;
  }
  }
bool findWord(string word, vector& lexicon) {
Â
  for(unsigned int i=0; i      if(lexicon[i] == word)
        return true;
  return false;
}
___________________________________________
Â
findWordsMain.cpp
Â
___________________________________________
#include
#include
#include
Â
using namespace std;
Â
int main(int argc, char *argv[0]) {
 Â
  if (int i=0; i   vector lexicon;
  string word = "poodle";
  loadLexicon(lexicon, "Enhanced North American Benchmark Lexicon.txt" );
  if(findWord(word, lexicon))
     cout << word << " is a word.\n";
  else
     cout << word << " is NOT a word. \n";
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll