Question

C++ Part B: (String) Program Description: Write a word search program that searches an input data...

C++

Part B: (String)

Program Description:

Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following functions :

void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount) ; (15%)

void displayResult(string word, int wordCount, int grammaticalCount); (15%)

Both functions should be called from main(). No non-constant global variables should be used. (20%)

Test your program using the file provided “paragraph.dat”.

paragrap.dat

You'll generally read and write longer paragraphs in academic papers. However, too many long
paragraphs can provide readers with too much information to manage at one time. Readers
need planned pauses or breaks when reading long complex papers in order to understand your presented
ideas. Remember this writing mantra: "Give your readers a break!" or
"Good paragraphs give one pause".

Homework Answers

Answer #1

Note: I am assuming that the word is checked for exact match, ie, 'read' will match with 'read' but not 'reader'. Also by grammatical count i am assuming that it stands for the number of characters in the words.

#include <iostream>
#include <string>
#include <fstream> // header file to work with files
using namespace std;

void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount)
{
    string line;
    if (inFile.is_open()) // to check if the file is open
    {
        while (inFile)
        {
            inFile >> line;                    // check next word
            grammaticalCount += line.length(); // adding the length of the word to the grammatical count
            if (line.compare(wordSearch) == 0) // comparing the word to the input word
                wordCount++;                   // if same, then count is increased
        }
    }
}

void displayResult(string word, int wordCount, int grammaticalCount)
{
    cout << word << " appears " << wordCount << " times and the file has " << grammaticalCount << " grammatical count.";
}

int main()
{
    ifstream inFile;
    string file, wordSearch;
    cout << "Enter the name of the file: ";
    cin >> file;
    cout << "Enter the word to search: ";
    cin >> wordSearch;
    inFile.open(file); // opening the file
    int wordCount = 0, grammaticalCount = 0;
    processFile(inFile, wordSearch, wordCount, grammaticalCount); // calculating the counts
    displayResult(wordSearch, wordCount, grammaticalCount);       // displaying the output
    inFile.close();                                               // closing file
}

Test output:

Enter the name of the file: paragraph.dat
Enter the word to search: one
one appears 2 times and the file has 335 grammatical count.
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
write a c++ program that can take an input file with string as for example "i...
write a c++ program that can take an input file with string as for example "i saw 5 teddy bears" and in a created output file create a new string to just change a digit to a word. To " I saw five teddy bears" . output should be located in an output file thank you
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...