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 Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
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 a C++ Program Consider the following incomplete C++ program: #include <iostream> using namespace std; int...
Write a C++ Program Consider the following incomplete C++ program: #include <iostream> using namespace std; int main() {    …. } Rewrite the program include the following statements. Include the header file fstream, string, and iomanip in this program. Declare inFile to be an ifstream variable and outFile to be an ofstream variable. The program will read data from the file inData.txt and write output to the file outData.txt. Include statements to open both of these files, associate inFile with...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file For example, given the following CSV file input1.csv: 1,10,20,30,40 The output of your program must be: 5 You can safely assume that the input file will be a...
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...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the following CFG: 1. exps --> exp | exp NEWLINE exps 2. exp --> term {addop term} 3. addop --> + | - 4. term --> factor {mulop factor} 5. mulop --> * | / 6. factor --> ( exp ) | INT The 1st production defines exps as an individual expression, or a sequence expressions separated by NEWLINE token. The 2nd production describes an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT