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

60.1K

Verified Solution

Question

Programming

  1. Read an unsorted keywords file once to determine how many wordsare in the file.
  2. Allocate memory dynamically to store the unsorted keywords inan array of strings or an array of c-strings. (Hint: be sure toclear your input file stream before re-reading the file)
  3. Reread the keywords file a second time and store the words inthe dynamically allocated array of strings or c-strings
  4. Sort the array of key words. (Hint: be sure to check yoursorted array of key words - there should be 84)
  5. Search a C++ program input file for occurrences of thekeywords:
    • Read one line at a time from the C++ program file into acstring. (Hint: use the istream::getline function - see examplebelow)
    • Parse each line into separate words. Ignore any words that areinside a C++-style comment. (Hint: use strtok)
    • Search the keyword array to determine if each word is a keyword(Hint: use a binary search or a sequential search)
    • For keywords, print the line number, the keyword, and theposition of the keyword in the line. (Hint: use the difference oftwo pointers)
    • Keep a count of the number of keywords found

Program Requirements

  • This zippedfile(http://voyager.deanza.edu/~bentley/cis22b/ass4files.zip)contains the keywords file and the C++ program file forsearching.
  • Use a char array to hold each line from the C++ program file.“Parse” each line into individual words for searching. Note, youmay not use the stringstream classes for this assignment.
  • Make sure you check the input file for successful opens.
  • Your output should match the format show below with the correctline number and position of each word in the line. The linecharacter positions start at zero. Note, there are more than 50lines of output.

Program Output

Your output should look like this:

Line 8: using(0) namespace(6)   <== usingoccurs at position 0 on line 8, namespace occurs at position 6 online 8
Line 10: const(0) int(6)
Line 12: void(0) const(19)
Line 13: void(0) char(20) int(32) const(48)
Line 14: bool(0) const(24) char(30) const(42)
Line 15: void(0) char(17)
Line 16: void(0)
Line 17: void(0)
Line 19: int(0)
Line 21: const(4)
...
Number of keywords found = ??   <== Add this lineat the end of your output, replace ?? with the correct number

Program Hints

  • Follow the program steps. Write only one part of the program ata time. Test each part before you proceed to the next step. Do notcontinue if one part has a problem. Ask for help with a step, ifyou can't get it to work. Remember to allow plenty of time for thisassignment.
  • Use a small keyword file and a small test C++ program fileinitially 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 \"parseout\" 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 isbecause strtok() destroys the original line.
  • Xcode users: There is a \r at the end of each line in thetest file. You can suppress it by adding \"\r\" as adelimiter for strtok().
  • Your program should produce more than 50 lines of output andyou should find more than 70 keywords (many are repeats).

The keyword file looks like this:

for
if
nullptr
break
int
long
sizeof
return
short
else
friend
const
static_cast
...

The C++ program file looks like this:

#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);
...


istream::getline example

ifstream fin(\"oldass3.cpp\");
...
char buffer[132];
...
fin.getline(buffer,sizeof(buffer)); // store a line from the inputfile into buffer

Answer & Explanation Solved by verified expert
4.3 Ratings (779 Votes)
Codeinclude include include include    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students