In: Computer Science
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".
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 :
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 .