4 - Search for C++ keywords
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.
- 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. (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.
- 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
- Read one line at a time from the C++ program file.
- Parse each line into separate words. Ignore any words that are inside a C++-style comment.
- Search the keyword array to determine if each word is a
- For keywords, print the line number, the keyword, and
the position of the keyword in the line.
- Keep a count of the number of keywords found
- 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.
output should looks like this:
|Line 8: using(0) namespace(6)
<== using occurs
at position 0 on line 8, namespace occurs
at position 6 on line 8
void(0) char(20) int(32) const(48)
bool(0) const(24) char(30) const(42)
Line 19: int(0)
keywords found = ??
Add this line at the end of your output, replace ?? with the correct
- 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".
- 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
- 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
- Your program should produce more that 50 lines of output
and you should find more than 70 keywords (many are repeats).
keyword file looks like this:
C++ program file looks like this:
DictionarySize = 23907;
getDictionary(const string& filename,string*);
spellCheckLine(char* line, int lineNumber, const string* dictionary);
wordIsInDictionary(const char* word, const string* dictionary);