63 140
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #24801 in C++ for ANAND VENKATA SATISH

Question #24801
write a function to count the presence of words he and she & the number of line contains words he and she in a text file "story.txt"&
Expert's answer
#include<iostream>
#include<fstream>

using namespace std;

void heSheCounter();

int main()
{
heSheCounter();

return EXIT_SUCCESS;
}

void heSheCounter()
{
//read from file
ifstream fin("story.txt");
if(!fin.is_open())
{
cout<<"Could not open file";
return;
}


//initialize counter variables
int heCounter = 0;
int sheCounter = 0;
int lineCounter = 0;

//initialize buffer(stack) for words & char for character
const int BUF_SIZE = 6;
char buffer[BUF_SIZE] = {0};
char curChar;

//continuous variable will determine the continuous sequence of characters
int continuous = 0;

//get file size
fin.seekg (0, ios::end);
int fileSize = fin.tellg();
fin.seekg (0, ios::beg);

int curPos = fin.tellg();//current cursor position
char prevChar,nextChar; // e.g prevChar'He'nextChar
bool wasHeSheinLine = false;

while(fin.good())//read from file while it's possible
{
curChar = fin.get();//read next character

if( fin.tellg() <= 1 ||
tolower(curChar) == 's'& ||
tolower(curChar) == 'h'& ||
tolower(curChar) == 'e')
{
buffer[continuous] = curChar;
if(continuous == 1)//is he ?
{
if(stricmp(buffer,"he") == 0)// if he
{
//check if this is a word but not a piece of another word
curPos = fin.tellg();

if(fin.tellg() > 2)//if he is first word , maybe it he
{
fin.seekg(curPos-3,ios::beg);
prevChar = fin.get();//read char prev to word he
}
else
prevChar = '1';//not alphabetic

fin.seekg(curPos,ios::beg);//back to current pos

nextChar = fin.get();//read next char
fin.seekg(curPos,ios::beg);//return current pos

if(!isalpha(prevChar) && !isalpha(nextChar))//if ambient chars is not aplphabetics
{
heCounter++;
continuous = 0;
memset(buffer,0,BUF_SIZE);//clear the buffer
wasHeSheinLine = true;
continue;
}
else
{
continuous = 0;
memset(buffer,0,BUF_SIZE);//clear the buffer
continue;
}
}
}

if(continuous == 2)//is she ?
{
if(stricmp(buffer,"she") == 0)// if she
{
//check if this is a word but not a piece of another word
curPos = fin.tellg();

if(fin.tellg() > 3)//if he is first word , if it she !
{
fin.seekg(curPos-4,ios::beg);
prevChar = fin.get();//read char prev to word she
}
else
prevChar = '1';//not alphabetic

fin.seekg(curPos,ios::beg);//back to current pos

nextChar = fin.get();//read next char
fin.seekg(curPos,ios::beg);//return current pos

if(!isalpha(prevChar) && !isalpha(nextChar))//if ambient chars is not aplphabetics
{
sheCounter++;
continuous = 0;
memset(buffer,0,BUF_SIZE);//clear the buffer
wasHeSheinLine = true;
continue;
}
else
{
continuous = 0;
memset(buffer,0,BUF_SIZE);//clear the buffer
continue;
}
}
}
continuous++;
continue;
}

if(curChar == '\n' && wasHeSheinLine == true)// if was he or she increment lineCounter
{
lineCounter++;
wasHeSheinLine = false;//set to "not encounter he she yet"
}

continuous = 0;
memset(buffer,0,BUF_SIZE);//clear the buffer
}

if(wasHeSheinLine)
lineCounter++;//check in last line

//output results
cout<<"Number of \"he\" words in text is : \t\t\t"<<heCounter<<endl;
cout<<"Number of \"she\" words in text is : \t\t\t"<<sheCounter<<endl;
cout<<"Number of lines contain \"he\" or \"she\" words is : \t"<<lineCounter<<endl;

cin.get();//suspend the console
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions