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

#include <iostream> #include <fstream> #include <cassert> #include <cstring> #in

ID: 3568619 • Letter: #

Question


#include <iostream>
#include <fstream>
#include <cassert>
#include <cstring>
#include <cctype>
using namespace std;

// Depending on the computer, if the values below are too large,
// your program will cause a "segmentation fault." I suggest
// you make the value of MaxLength something smaller in this case.
const int MinLength = 3; // minimum word size
const int MaxLength = 10; // maximum word size
const int Rows = 50000; // max array size to handle all words on Unix system

//-----------------------------------------------------------------------------------
// Convert word to lower case
void convertToLowerCase( char theWord[], const int size)
{
// convert dictionary word to lower case
for (int i=0; i<size; i++) {
theWord[i] = tolower(theWord[i]);
}
}


//-----------------------------------------------------------------------------------
// Reverses the string Note:changing the orginal string
char * strReverse(char *str)
{
char *p1, *p2;

if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}

//-----------------------------------------------------------------------------------
// Read from the dictionary file into an array, only keeping words that are the correct
// length.
void readDictionary(
   char words[][ MaxLength+1],    // array of dictionary words
   int &currentRow)           // number of words in dictionary
{
   ifstream inStream;                    // input file stream
   char fileName[] = "dictionary.txt";   // Input file name
   char tempString[MaxLength+1];   // stores a single string
   int size;           // string size

inStream.open( fileName);
assert( ! inStream.fail() ); // make sure file open was OK

   cout << " Reading dictionary file from " << fileName << " ";
   currentRow = 0;
   while ( inStream >> tempString ) {
        size = strlen( tempString);
convertToLowerCase( tempString, size); // convert word to lower case
        // Only store words that aren't too long or too short
if ( (size <= MaxLength ) && (size >= MinLength) ) {
           strcpy( strReverse(words[ currentRow++]), tempString);
}
       // Verify that we still have room in array
if (currentRow >= Rows) {
cout << "*** Error, just read word " << tempString
<< " and currentRow is now at: " << currentRow
<< endl;
}
       assert( currentRow < Rows); // halt program if error
   }

inStream.close(); // close the input file stream

}


//-----------------------------------------------------------------------------------
// Display words within the boundaries of a certain size.
// This should only be used for debugging.
void displayWords(
   char theWords[][ MaxLength+1],    // array of dictionary words
   int limit)           // number of words in dictionary
{
   int displaySizeMax;       // Max size string to display
   int displaySizeMin;       // Min size string to display
   int size;   // stores size of strings

cout << "(Stored words are of size " << MinLength << " to " << MaxLength << ") ";
   cout << "Please enter min. and max. sizes of strings to display: ";
   cin >> displaySizeMin >> displaySizeMax;

   cout << " Words stored in array are: ";
   for (int i=0; i<limit; i++) {
       size = strlen( theWords[ i]);
       if ( (size >= displaySizeMin) && (size <= displaySizeMax) )
           cout << theWords[ i] << " ";
   }
   cout << " ";       // leave an extra line at the end
}


//-----------------------------------------------------------------------------------
// Look up the word in the dictionary array, returning true if found, false otherwise
bool wordWasFound(
char pWord[], // the word for which we are searching
char dictionary[][ MaxLength + 1], // dictionary array
int numberOfDictionaryWords) // number of words in dictionary array
{
printf(" Word that I am about to compare %s ", pWord);
   bool printed = false;
   for (int i=0; i< numberOfDictionaryWords; i++)
   {

//printf("Dictionary[%d] = %s BEFORE strncmp ", i, dictionary[i]);//EDIT THIS CODE
if( strncmp( pWord, strReverse(dictionary[i]), strlen(pWord)) == 0)
       {
strReverse(dictionary[i]); //Reverse word back to original
        // printf("Dictionary[%d] = %s AFTER strncmp ", i, dictionary[i]);

cout << dictionary[i]<< " is rhyming with the given word. ";
// Found the word!
printed =true;
}

}
   return printed; // Did not find the word

   //cout<< "word wasnt found. ";

}


//-----------------------------------------------------------------------------------
// Main function
int main()
{
   char dictionary[ Rows][MaxLength+1]; // array of dictionary words
   int numberOfWords= 0;           // number of dictionary words

   // Read words from input file into dictionary array
    readDictionary( dictionary, numberOfWords);

   // Selectively display words from dictionary array
   displayWords( dictionary, numberOfWords);

// Lookup a word in the dictionary using binary search
char aWord[ MaxLength];
string bWord;
cout << "Enter a word for which to search. (Max length is " << MaxLength << ") ->";
cin >> aWord;
//strReverse(aWord); //String reversal nevermind!!!

cout << aWord << endl;

convertToLowerCase( aWord, strlen( aWord) ); // Ensure input is lower case
strReverse(aWord);//reverses the c-string
//string tmpStr(aWord);//Converts into string
//bWord=tmpStr.substr(0,3);// takes in the first three letters of the reversed string...viz. the last three letters of the original string
cout << bWord << endl;

//display appropriate message depending on whether or not the word is found
if ( wordWasFound( aWord, dictionary, numberOfWords) ) {
cout << "This rhyming word IS in the dictionary. ";
}
else {
cout << "This rhyming word is NOT in the dictionary. ";
}

return 0;
}

Hi here is my code.

I want to insert all of words from dictionary.txt in the trie.

Can you show me the functions for it? and where to put those function

Explanation / Answer

use this method to read data from txt file

And finally call inserrt method