C++ File Handling|Reading File word by word

Reading File word by word

Question:12
Write a function in c++ named count_the() to read a file named “story.txt”. Count and print total number of “the” present in the file file.
Sol:

/* count total words "the" in the file */
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void count_the()
{
	ifstream pqr;
	char word[20];
	int c=0;
	pqr.open("story.txt");
	if (pqr.fail())
	{
		cout<<"Unable to open the file"<<endl;
		exit(1);
	}
	while(!pqr.eof())
	{
		pqr>>word;
		if(strcmpi(word,"the")==0)
			c++;
	}
	pqr.close();
	cout<<"total count "<<c<<endl;
}
int main()
{
	/* clrscr(); */
	count_the();
	return(0);
}

Question: 13
Write a function in c++ named count_these() to read a file named “article.txt”. Count and print total number of “these” present in the file file.
Sol:

/* count total words "the" in the file */
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void count_the()
{
	ifstream pqr;
	char word[20];
	int c=0;
	pqr.open("article.txt");
	if (pqr.fail())
	{
		cout<<"Unable to open the file"<<endl;
		exit(1);
	}
	while(!pqr.eof())
	{
		pqr>>word;
		if(strcmpi(word,"these")==0)
			c++;
	}
	pqr.close();
	cout<<"total count "<<c<<endl;
}
int main()
{
	/* clrscr(); */
	count_the();
	return(0);
}

Question: 14
Write a function in c++ named count_is() to read a file named “article.txt”. Count and print total number of “is” present in the file file.
Sol:

/* count total words "is" in the file */
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void count_is()
{
	ifstream pqr;
	char word[20];
	int c=0;
	pqr.open("article.txt");
	if (pqr.fail())
	{
		cout<<"Unable to open the file"<<endl;
		exit(1);
	}
	while(!pqr.eof())
	{
		pqr>>word;
		if(strcmpi(word,"is")==0)
			c++;
	}
	pqr.close();
	cout<<"total count "<<c<<endl;
}
int main()
{
	/* clrscr(); */
 	count_is();
	return(0);
}