Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

wrie·C++ Prvgram: Impletet a Lexicon class using lists. This Leucon class will m

ID: 3727441 • Letter: W

Question

wrie·C++ Prvgram: Impletet a Lexicon class using lists. This Leucon class will maintain a list of words, and the user will be ablie to quacry the Lexicon as to whether or not a wool is inthe Lexicon, the us f will be able to bdd weeds to the Lexicon. or to modify a word in the Lexcon Lexicoe Lexiconl const string& fileName words in the file in the Lexicon ifthe fik docsn't exis, an exception should be thrown file and not aC++string object. To get the C-string oquivalent ef any given C++ string. A constructor that allows the wser to creatc an empty Lexicon A consuctoe that opens the file associated with fileName, and stores all of the N B. The ifse am coestructor takes a C-style string as a parameter to open the usg the member function For eample, so get the C-tring equivalont of fWe Name list string startWithchar ch): liststring stWit const string&prefix; list string endWith char ch): listkstring endWith const string&suffis;) void addWordcon sring&s; void updateWordiconst string⌖, const string& replacement) retums true if wond is in the Lexicon, and false otherwise. retums a list a ofwords that art with a particular letter, ck retums a list of woeds that start with profix retums list of words that end with a particult character, ck ret ms a list ofwords that end with , Adds str to the Lexicon if in is not there already. If it is there already, do nothing Changes the spelling of farget to bocome replacement Operaters friend bool operator (coest Lexiconk di, const Lescon&d;ž retums true iff dl and d2 contain the same words, not necessanily in the same oeder. IN B be careful berc. You can not just assume that you can compare the lexioons word by word, since they may mot be stored in the same ordkr) friend operaor ost&, const Lexiconk dk Outputs the comtent ofthe Lexicem (in any order Submit Lesicon h.Lesicon opp, and useLexicoe.cpp files. Also, please use comments to document your code I have peovided 2 text files with the same lexicon, but I ran a pogram that has randomly pemuted the words such th they are no longer listed in sorted ender. When se files have been sMond into Lexicon objocts, your function should retum true

Explanation / Answer

//lexicon.h

#include <bits/stdc++.h>
using namespace std;

class lexicon
{
   private :
   list<string> words;
   public :
   lexicon()
   {
       // nothing
   }
   lexicon(const string &filename)
   {
       // initialize list from file
       char* chr = const_cast<char*>(filename.c_str());
       ifstream file;
        file.open ("filename");
        string word;
        char x ;
        word.clear();
        while ( ! file.eof() )
        {
            x = file.get();
            while ( x != ' ' )
            {
                word = word + x;
                x = file.get();
            }
            cout<< word <<endl;
            words.insert(word);
            word.clear();
        }
   }
   void add_word(const string &str);
   void update_word(const string &target, const string &replacement);
   bool contains_word(const string &str);
   list<string> startwith(char ch);
   list<string> endwith(char ch);
   list<string> endwithsuufix(const string &str);
   list<string> startwithprefix(const string &str);
};


// lexicon.cpp

#include "lexicon.h"

void lexcion :: add_word(const string &str)
{
   words.insert(str);
   return ;
}

void lexcion :: update_word(const string &target, const string &replacement)
{
   std::list<string>::iterator it;
   for(it=words.begin();it!=words.end();it++)
   {
       if((*it).compare(target) == 0)
           *it = replacement ;
   }
}

bool lexcion :: contains_word(const string &str)
{
   std::list<string>::iterator it;
   for(it=words.begin();it!=words.end();it++)
   {
       if((*it).compare(str) == 0)
           return true;
   }
   return false;
}

list<string> lexcion :: startwith(char ch)
{
   list<string> temp;
   std::list<string>::iterator it;
   for(it=words.begin();it!=words.end();it++)
   {
       if((*it)[0] == ch)
           temp.insert(*it);
   }
   return temp;
}

list<string> lexcion :: endwith(char ch)
{
   list<string> temp;
   std::list<string>::iterator it;
   for(it=words.begin();it!=words.end();it++)
   {
       int len = strlen((*it));
       if((*it)[len-1] == ch)
           temp.insert(*it);
   }
   return temp;
}

list<string> lexcion :: startwithprefix(const string &str)
{
   list<string> temp;
   std::list<string>::iterator it;
   int len2 = strlen(str);
   int i;
   bool flag = true;
   for(it=words.begin();it!=words.end();it++)
   {
       int len = strlen((*it));
       flag = true;
       if(len < len2)
           continue;
       for(i=0;i<len2;i++)
       {
           if(str[i] != (*it)[i])
           {
               flag = false;
               break;
           }
       }
       if(flag == true)
           temp.insert(*it);
   }
   return temp;
}

list<string> lexcion :: endwithsuufix(const string &str)
{
   list<string> temp;
   std::list<string>::iterator it;
   int len2 = strlen(str);
   int i;
   bool flag = true;
   for(it=words.begin();it!=words.end();it++)
   {
       int len = strlen((*it));
       flag = true;
       if(len < len2)
           continue;
       for(i=0;i<len2;i++)
       {
           if(str[len2-i-1] != (*it)[len-i-1])
           {
               flag = false;
               break;
           }
       }
       if(flag == true)
           temp.insert(*it);
   }
   return temp;
}