Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program Steps 1. Read an unsorted keywords file once to determine how many words are in the file. 2. Allocate memory dynamically to store the

Program Steps

1. Read an unsorted keywords file once to determine how many words are in the file.

2. Allocate memory dynamically to store the unsorted keywords in an array of C strings. (Hint: be sure to clear your input file stream before re-reading the file)

3. Reread the keywords file a second time and store the words in the dynamically allocated array of C strings.

4. Sort the array of key words. (Hint: be sure to check your sorted array of key words - there should be 84)

5. Search a C++ program input file for occurrences of the keywords:

a. Read one line at a time from the code file.

b. Parse each line into separate words. Ignore any words that are inside a comment.

c. Search the keyword array to determine if each word is a keyword.

d. For keywords, print the line number, the keyword, and the position of the keyword in the line.

e. Keep a count of the number of keywords found

Program Requirements

1. This zipped file contains the keywords file and the input code file for searching.

2. Use a char array to hold each line from the input code file. Parse each line into individual words for spell checking. Note, you may NOT use the stringstream classes for this assignment.

3. Make sure you check the input file for successful opens.

4. Your output should match the format show below with the correct line number and position of each word in the line. The line character positions start at zero. Note, there are more than 50 lines of output.

Hints

Use a small keyword file and a small test C++ code file initially as you are developing your code.

Use strstr() to find the // of a C++-style comment.

Use strtok() for the parsing of each line. You should "parse out" much of the program "punctuation".

You might want to make a copy of each line (maybe as a string) to determine the position of the keyword in the line. This is because strtok() destroys the original line.

Your program should produce more that 50 lines of output and you should find more than 70 keywords (many are repeats).

KEYWORDS for if nullptr break int long sizeof return short else friend const static_cast switch not auto extern and double case enum false and_eq float explicit decltype bitand continue dynamic_cast goto mutable new bitor inline bool catch namespace char compl asm const_cast not_eq operator or class or_eq private export public typedef protected typeid register static reinterpret_cast template this struct throw true try default delete do typename union unsigned using virtual void volatile wchar_t while xor xor_eq alignas alignof char16_t char32_t constexpr signed noexcept static_assert thread_local

INPUT CODE

#include

#include

#include

#include

#include

#include

#include

using namespace std;

const int DictionarySize = 23907;

void getDictionary(const string& filename,string*);

void spellCheckLine(char* line, int lineNumber, const string* dictionary);

bool wordIsInDictionary(const char* word, const string* dictionary);

void toLowerCase(char* text);

void toLowerCase(string& text);

void insertion_sort(string* list);

int main()

{

const string wordfile = "c:/temp/unsorted_words.txt";

const string testfile = "c:/temp/gettysburg.txt";

char buffer[1024];

string word;

string* dictionary = new string[DictionarySize];

getDictionary(wordfile, dictionary);

// sort the dictionary

insertion_sort(dictionary);

int lineNumber = 0;

// read a process file to be spell checked

ifstream fin(testfile.c_str());

if (!fin)

{

cerr << "Unable to open word file " << testfile << endl;

exit(2);

}

while (!fin.eof())

{

lineNumber++;

fin.getline(buffer,sizeof(buffer));

if (fin.eof()) break;

toLowerCase(buffer);

spellCheckLine(buffer,lineNumber, dictionary);

}

delete [] dictionary;

}

void spellCheckLine(char* line, int lineNumber, const string* dictionary)

{

char* ptr = line;

ptr = strtok(line," ,. ");

while(ptr != NULL)

{

if(!wordIsInDictionary(ptr, dictionary))

{

cout << "Misspelled word, " << ptr << " on line " << lineNumber << endl;

}

ptr = strtok(NULL," ,.- ");

}

}

void getDictionary(const string& filename, string* dictionary)

{

ifstream fin(filename.c_str());

if (!fin)

{

cerr << "Unable to open word file " << filename << endl;

exit(1);

}

cout << "Reading the Dictionary File ..." << endl;

for (int i = 0; i < DictionarySize; i++)

{

fin >> dictionary[i];

toLowerCase(dictionary[i]);

}

}

bool wordIsInDictionary(const char* word, const string* dictionary)

{

int low, high, middle;

string word_string(word);

low = 0;

high = DictionarySize - 1;

string middleWord;

int compare;

while (low <= high)

{

middle = (low + high) / 2;

middleWord = dictionary[middle];

compare = strcmp(word,middleWord.c_str());

if (compare < 0)

{

high = middle - 1;

}

else if (compare > 0)

{

low = middle + 1;

}

else

{

return true;

}

}

// look for misspelled word with an 'ed' ending

if (word_string.substr(word_string.size() - 2, 2) == "ed")

{

if (wordIsInDictionary(word_string.substr(0, word_string.size() - 2).c_str(), dictionary))

return true;

}

// look for misspelled word with an 'ed' ending

if (word_string.substr(word_string.size() - 2, 2) == "ly")

if (wordIsInDictionary(word_string.substr(0, word_string.size() - 2).c_str(), dictionary))

return true;

// look for misspelled word with an 's' ending

if (word_string.substr(word_string.size() - 1, 1) == "s")

if (wordIsInDictionary(word_string.substr(0, word_string.size() - 1).c_str(), dictionary))

return true;

return false;

}

void toLowerCase(char* text)

{

for (size_t i = 0; i < strlen(text); i++)

text[i] = tolower(text[i]);

}

void toLowerCase(string& text)

{

for (size_t i = 0; i < text.size(); i++)

text[i] = tolower(text[i]);

}

void insertion_sort(string* list)

{

int j, k;

string temp;

//char temp[32];

bool found;

cout << "Sorting the dictionary ..." << endl;

for (int i = 1; i < DictionarySize; i++)

{

// The ith element will be inserted into the "sorted" list in the correct location

found = false;

temp = list[i]; // temp will hold the ith element in the "unsorted list

for (k = 0, j = i - 1; j >= 0 && !found; k++)

{

// if temp < the jth element, move it to the j+1st position

if (temp < list[j])

{

list[j + 1] = list[j];

j--;

}

else // temp is not less than any other elements in the sorted list

{

found = true;

}

}

// insert temp into its correct position in the sorted list

list [j + 1] = temp;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions