Question

In: Computer Science

Done in c++, Read an unsorted keywords file once to determine how many words are in...

  1. Done in c++, 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. no victor use if possible.
  6. 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

Program Requirements

  • This zipped file(http://voyager.deanza.edu/~bentley/cis22b/ass4files.zip) 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".
  • 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.
  • 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

Solutions

Expert Solution

Code:

#include <iostream>

#include <fstream>

#include <string>

#include <sstream>

#include <algorithm>

#include <vector>

using namespace std;

int main()

{

     string testline;

     string codeline;

     vector<string> word;

     vector<string> app;

//opening keywords text file

     ifstream Test("keywords.txt");

     if (!Test)

     {

          cout << "There was a problem opening the file. Press any key to close.\n";

          return 0;

     }

    

     while (Test)

     {

          Test >> testline;

          //cout << testline << endl;

          word.push_back(testline);

     }

    

     //Dynamically allocating keyords to array

     for (int i = 0;i < word.size() - 1;i++)

      //cout << word[i] << "(" << i << ")" << endl;

     //sorting the keywords

     sort(word.begin(), word.end());

     //for (string &s : word)

          //cout << s << "\n ";

     cout << "output" << endl;

     //opening the input file

     ifstream code("input.txt");

     if (!code)

     {

          cout << "There was a problem opening the file. Press any key to close.\n";

          return 0;

     }

     int n = 0;

     int count = 1;

     int map = 0;

     int g = 0;

     bool c = true;

     while (code)

     {

         

     code >> codeline;

         

          for (int j = 0;j < word.size();j++)

          {

              if (code.peek() == '\n')

              {

                   count = count + 1;

                   n = 0;

                   if (word[j] == codeline)

                   {

                        c = true;

                        cout << "line[" << count << "]";

                        cout << codeline << "[" << n << "]";

                        cout << endl;

                        //   n = n+sizeof (codeline);

                        n = n + codeline.length();

                        map = map + 1;

                        break;

                   }

                   else

                        c = false;

                   break;

              }

              else

              {

                   if (word[j] == codeline)

                   {

                        c = true;

                        cout << "line[" << count << "]";

                        cout << codeline << "[" << n << "]";

                        cout << endl;

                  

                        n = n + codeline.length();

                        map = map + 1;

                        break;

                   }

                   else

                        c = false;

              }

          }

          if (c == false && n != 0)

              n = n + codeline.length();

    

     }

     cout << "Total no of keywords found" << map << endl;

     system("pause");

     return 0;

}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Read an unsorted keywords file once to determine how many words are in the file. Allocate...
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...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
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...
Done in c++ Read this file one time to determine how many records it contains. Be...
Done in c++ Read this file one time to determine how many records it contains. Be sure to check for a successful file open here. Close the file. Allocate memory dynamically to store the data from step 1. This dynamically allocated memory should be for an array of strings. The array size should be exactly the number of records from step 1. Reopen the file and read it a second time, storing each record into the array of strings from...
Read the words in from the binary file and figure out how many times each word...
Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user. Use ObjectInputStream to read binary file Use a HashMap with the word as a key (String) and an Integer as the value. For each word, first check to see if it already exists in the Map. If not, add the word as key with a value of 1 for the Integer value. If it...
C++ question: When cleaning a file the existing data is often read by character. Once a...
C++ question: When cleaning a file the existing data is often read by character. Once a file has been cleaned it is rarely read in by character. Explain Why?
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
How to count the number of words that only show up once in a text file,...
How to count the number of words that only show up once in a text file, and replace those words with a character '(unique)' using Python? Without list is better.
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT