Process the attached file of words to determine all the 7-letter words that star
ID: 3808119 • Letter: P
Question
Process the attached file of words to determine all the 7-letter words that start with "qu" or "ch" and have the same last 5 letters, such as "charter" and "quarter". Use the class template below. Fill out the assign_7ltr() function. Upload the result in standard format. Don't forget to name the upload file from your last name and the assignment, e.g. evans-7ltr.cpp. class Word { std::string word; // the word bool startqu; // true if the first 2 letters of the word are q and u bool startch; // true if the first two letters of the word are c and h int length; // length of the word Word *next; // a pointer to another instance of Word public: Word(std::string& w); // constructor bool startWithqu() const; // startqu accessor bool startWithch() const; // startch accessor bool prefixEqual(std::string& pfx) const; // is this word's prefix equal to the passed string bool suffixEqual(std::string& sfx) const; // is this word's suffix equal to the passed string Word *getNext() const; // next accessor Word *setNext(Word *n); // next set accessor std::string getWord() const; // word accessor }; Word::Word(std::string& w) { /* constructor */ /* initialize all member variables */ /* set the value of the startch and startqu flags */ } bool Word::startWithch() const { /* startch accessor */ } bool Word::startWithqu() const { /* startqu accessor */ } bool Word::prefixEqual(std::string& pfx) const { /* is this word's prefix equal to the passed string */ } bool Word::suffixEqual(std::string& sfx) const { /* is this word's suffix equal to the passed string */ } Word *Word::getNext() const { /* next accessor */ } Word *Word::setNext(Word *n) { /* next set accessor */ /* return the new value */ } std::string Word::getWord() const { /* word accessor */ } static int assign_7ltr() { const int TARGET_LENGTH = 7, START = 2; std::string fileName = "/Users/danevans/Downloads/7ltr-words.txt"; std::ifstream file; std::string word, sfx; std::string sent = "sentinel"; Word *chwords, *quwords, *qusq, *chsq, *obj; int chsiz, qusiz, count; file.open(fileName); if (!file) { cout << "unable to open file: " << fileName << endl; return -1; } /* create sentinel words to start the ch list and the qu list */ /* read and process each word */ while (getline(file, word)) { /* filter out words whose length is not seven letters */ /* construct a Word object from the input word */ /* if the Word instance starts with qu, add it to the qu list */ /* else if the Word instance starts with ch, add it to the ch list */ /* else free the object since it doesn't start with qu or ch */ } /* finish with the file */ /* drop and free the sentinel on each of the lists */ /* loop through the ch list */ while (0 != chsq) { /* get the 5 charactder suffix of the current ch Word */ /* loop through the qu list */ while (0 != qusq) { /* if the qu suffix is equal to the ch suffix, count it and display the words */ } } /* display the results of the searches */ cout << "found " << (chsiz + qusiz) << " seven letter words starting with "qu" or "ch"" << endl; cout << "found " << count << " matches" << endl; /* free the ch list */ /* free the qu list */ return 0; }
Explanation / Answer
class Word
{
std::string word; // the word
bool startqu; // true if the first 2 letters of the word are q and u
bool startch; // true if the first two letters of the word are c and h
int length; // length of the word
Word *next; // a pointer to another instance of Word
public:
Word(std::string& w); // constructor
bool startWithqu() const; // startqu accessor
bool startWithch() const; // startch accessor
bool prefixEqual(std::string& pfx) const; // is this word's prefix equal to the passed string
bool suffixEqual(std::string& sfx) const; // is this word's suffix equal to the passed string
Word *getNext() const; // next accessor
Word *setNext(Word *n); // next set accessor
std::string getWord() const; // word accessor
};
Word::Word(std::string& w)
{
word = w;
startqu = false;
startch = false;
length = 0;
}
bool Word::startWithch() const
{
char ch[100];
strncpy(ch,word,2);
if(ch[0] == 'c' && ch[1] == 'h')
{
return true;
}
else
return false;
}
bool Word::startWithqu() const
{
char ch[100];
strncpy(ch,word,2);
if(ch[0] == 'q' && ch[1] == 'u')
{
return true;
}
else
return false;
}
bool Word::prefixEqual(std::string& pfx) const
{
if(strcmp(pfx,word) == 0)
return true;
else
return false;
}
bool Word::suffixEqual(std::string& sfx) const
{
if(strcmp(sfx,word) == 0)
return true;
else
return false;
}
Word *Word::getNext() const
{
return *next;
}
Word *Word::setNext(Word *n)
{
*next = n;
return *next;
}
std::string Word::getWord() const
{
return word;
}
static int assign_7ltr()
{
const int TARGET_LENGTH = 7, START = 2;
std::ifstream file;
std::string word, sfx;
std::string sent = "sentinel";
Word *chwords, *quwords, *qusq, *chsq, *obj;
int chsiz, qusiz, count;
file.open ("testfile.txt");
if (!file)
{
cout << "unable to open file: "<< endl;
return -1;
}
/* create sentinel words to start the ch list and the qu list */
/* read and process each word */
while (getline(file, word))
{
if(strlen(word) == TARGET_LENGTH)
{
Word wordobj = word;
char ch[100];
strncpy(ch,wordobj,START);
if(ch[0] == 'c' && ch[1] == 'h')
{
*chwords.setNext(wordobj);
}
else
if(ch[0] == 'q' && ch[1] == 'u')
{
*quwords.setNext(wordobj);
}
else
{
wordobj = '/0';
}
}
}
while (0 != chsq)
{
/*char ch[100];
strncpy(ch,wordobj,5);
loop through the qu list */
while (0 != qusq)
{
if(strcmp(chsq,qusq) == 0)
{
count++;
cout << "found " << chsq << qusq << endl;
}
}
}
/* display the results of the searches */
cout << "found " << (chsiz + qusiz) << " seven letter words starting with "qu" or "ch"" << endl;
cout << "found " << count << " matches" << endl;
/* free the ch list */
/* free the qu list */
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.