Assignment 4 - Search for C++ keywords

Updated 10/17
due 10/20

The purpose of this assignment is to give you practice using dynamic memory allocation, c-string functions, sorting, and searching.  You will write a word searching program and test it.

10/17 Zoom Session

Recording

Example: Reading a file using << and getline


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 strings or 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 strings or 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:
Warning for Mac and Linux compilers

If you are using a Mac or Linux compiler for this assignment, you may encounter a problem using the "string version" or the "C-string version" of the getline function.  The problem is that an extra "carriage return" character will be inserted into your string or C-string when you read either input file in the assignment.  This is a result of the creation of the input files on Windows and how a Mac or Linux compiler reads those files with getline.  This issue will be discussed in class next week, but for now here is a suggested fix.

For the "string version" of getline
string temp;
...
getline(fin,temp);                                            // read from input file into a string
if (temp.back() == '\r') temp.pop_back();                     // remove carriage return from string
...

For the "C-string version" of getline
char temp[132];
...
fin.getline(temp,sizeof(temp));                               // read from input file into a C-string
if (strlen(temp) > 0 && temp[strlen(temp)-1] == '\r') temp[strlen(temp)-1] = 0;   // remove \r
...

Program Requirements


Program Output

Your output should look like this:

Line 8: using(0) namespace(6)   <==  using occurs at position 0 on line 8, namespace occurs at position 6 on line 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 line at the end of your output, replace ?? with the correct number

Program Hints


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 <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
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 input file into buffer