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
- Read an unsorted keywords file once to determine how many
words are in the file.
- 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)
- Reread the keywords file a second time and store the words
in the dynamically allocated array of strings or c-strings
- Sort the array of key words. (Hint: be
sure to check your sorted array of key words - there should be 84)
- Search a C++ program input file for occurrences of
the keywords:
- Read one line at a time from the C++ program file into a cstring. (Hint: use the istream::getline function - see example below)
- Parse each line into separate words. Ignore any words that are inside 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
the position of the keyword in the line. (Hint: use the difference of two pointers)
- Keep a count of the number of keywords found
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
- This zipped file contains
the keywords file and the C++ program file for searching.
- Use a char array to hold each line from the C++ program
file. “Parse” each line into individual words for searching. Note, you may 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
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.
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
- Follow the program steps. Write only one part of
the program at a time. Test each part before you proceed to
the next step. Do not continue if one part has a problem.
Ask for help with a step, if you can't get it to work.
Remember to allow plenty of time for this assignment.
- Use a small keyword
file and a small test C++ program 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".
- Xcode users: There is a \r at the end of each line in the test
file. You can suppress it by adding "\r" as a delimiter for
strtok().
- Your program should produce more than 50 lines of output
and you 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
<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 |