Friday, 30 March 2018

Object Oriented Programming: No. 11


No: 11


1. Experiment vision: Write A C++ Program That Counts Number Of Words In A File.
2. OBJECTIVE: Program to implement counting number of words in a File
3. THEORY: In this prgram, we will find the total number of words in a file. For this we used file related manipulation functions. Initially we declare the fstream.h which is file related stream file in as a header file into our program. First we open the text file from which we have to count the number of words, using fopen() function. Then using seekg pointer we move the pointer from staring and counting the number of words till the end of file appears.
4. PROGRAM:
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
int count=1;
ifstream f1;
f1.open("student.txt");
f1.seekg(0);
char ch;
while(! f1.eof())
{
f1.get(ch);
}
if(ch==' '|| ch=='\t'|| ch=='\n')
{
count++;

}
cout<<"Number of words="<<count;
f1.close();
return 0;
}

  1. OUTPUT:


6. CONCLUSION: Thus using C++ program we have successfully executed the program for counting number of words.
7. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the experiment, and check the algorithm and program.
7. REMARK:
A template can be considered as a macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type.
8. Discussion Questions:
1) What is EOF?.
____________________________________________________________________________________________________________________________________________________________
2) What is the use of get()?
____________________________________________________________________________________________________________________________________________________________



No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.