Question

In: Computer Science

C++ Write a word search program that searches an input data file for a word specified...

C++

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 function:

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

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

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

Test using this txt file:

You prolly like eating lots of cake and stuff. But, if you eat

too much then you'll just be more prone to getting some cavities. Kids

especially like eating sweets and all sorts of candy cause I've never even met one that hasn't

lmao. Remember: "An apple a day keeps the doctor away!" or

"Good kids listen to their parents".

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for question . Be safe .

Note : check attached image for output .code compiled and tested in C++14 Code blocks IDE.

Program Plan :

  1. inside processFile function read each line using ifstream object
  2. now check each char between A-Z or a-z increment grammatical count.
  3. countWords function separate sentence by space and inside space seprated string
  4. we check for single quote for word like you'will
  5. so parse by ' and match word till inbuilt function find not return -1 .
  6. also toLower function return lowercase string of passed srtring

Program :

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include <ctype.h>
using namespace std;

void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount);
int countWord(string word,string line);
string toLower(string word);

int main(int argc, char *argv[])
{
    char fileName[]={"data.txt"}; //array of file name
    int wordCount,grammaticalCount;
    string wordSearch="you";

    ifstream inFile(fileName);

    if(!inFile)
    {
        cout<<"Can not open file "<<endl;
        return 0;
    }
    else
    {
        processFile(inFile,wordSearch,wordCount,grammaticalCount);
        cout<<wordSearch<<" word found "<<wordCount<<" times."<<endl;
        cout<<"File have "<<grammaticalCount<<" grammatical characters."<<endl;
    }


    //process_file(fileName);


    return 0;

}

//this function takes a string word
//return a new word with converted in lowercase string
string toLower(string word)
{
    string newWord=""; //declare a empty string
    for(int i=0;i<word.length();i++) //run a loop from 0 to length of word
    {
        newWord+=tolower(word[i]); //convert each char into lower case
    }
    return newWord; //return a new word
}


void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount)
{

        wordCount=0; //first set value to zero of word count
        grammaticalCount=0; //first set grammatical count to 0

        string line; //line string

        //get each line in line string
        while (getline(inFile,line)) //read line by line
        {
            //if line is not empty proceed
            if (!line.empty()) //if line is not empty
            {
                for(int i=0;i<line.length();i++) //run a loop from 0 to length of line
                {
                    unsigned char ch=(unsigned char)line[i]; //get ASCII value of char
                    if((ch>=65 && ch<=90) || (ch>=97 && ch<=122)) //check char between A-Z or a-z
                            grammaticalCount++; //increment grammatical count
                }

                //this function takes word to find and each line as string
                //return number of times word found
                //add count to word count
                wordCount+=countWord(wordSearch,line);

            }

    }

    inFile.close(); //close file
}

//function takes how many word appeared in each line of file
int countWord(string word,string line)
{
    int pos=0; //pos =0
    int count=0;
    while(1)
    {
        //separate each sentence by space
        pos=line.find(' '); //get index of space

        if(pos==-1) //if its single word sentence
        {
            if(toLower(line)==toLower(word)) //so if word equal to word increment count
                    count++;
            break; //break from loop
        }

        else //if pos not equal -1
        {
            string newWord=line.substr(0,pos); //get a new word from 0 to pos (index of space)
            int newPos=newWord.find("'"); //after get new pos for word you'will
            //if ' found
            if(newPos!=-1)
            {
                newWord=newWord.substr(0,newPos); //get new word for you'will new word = you
                if(toLower(newWord)==toLower(word)) //check word equal to new word increment count
                    count++;
            }
            else //if word not have ' You
            {
                //we have new word separated by space check if equal to word
                if(toLower(newWord)==toLower(word))
                    count++;
            }
            //remove space string from start
            line=line.substr(pos+1);
        }
    }
    //return how many time word appears
    return count;
}

Output :

'

Please up vote ,comment if any query .


Related Solutions

For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The words that need to be found are in the file 'WordList.txt'. There are 100 words to be found. You must find the location of the first and last letter of each word as well as the cardinal direction that describes the word's orientation. The location of first and last letters are to be described in terms of row and column, with indexing starting at...
in basic c++ program please!! Write a word search and word count program. Assign the following...
in basic c++ program please!! Write a word search and word count program. Assign the following text to a string constant. For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him.Whoever believes in him is not condemned, but whoever does not believe stands...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
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,...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT