Friday, August 4, 2017

CBSE Class 12 - Computer Science - C++ Practical Text File Processing - Snippet-8 (#cbseNotes)

C++ Practical
Text File Processing Snippet-8

CBSE Class 12 - Computer Science - C++ Practical Text File Processing - Snippet-8 (#cbseNotes)

Question :  Write a program in c++  having a function to calculate the average word size of a text file.

Answer:




// Question: WAP in c++ having a function to calculate
// the average word size of a text file.


#include <iostream.h>
#include <fstream.h>
#include <process.h>

int calculate(char filename[])
{ 
 fstream File;
 File.open(filename,ios::in);
 char a[20];
 char ch;
 int i=0,sum=0,n=0;
 while(File)
 {
  File.get(ch);
  a[i]=ch;
  i++;
  if((ch ==' ') || (ch== '.')||(ch == ',')||(ch=='\t')||(ch =='\n')||(ch == EOF))
  {
   a[i] = '\0';
   //cout << a << endl;
   i--;
   sum=sum +i;
   i=0; n++;
  }
 }
 cout << "Total alphabets: " << sum << "\tNum of Word:" << n << endl;
 return (sum/n);
}

int main()
{
 int avgwordsize = calculate("WORDS.DAT");
 cout<< "average word size is "<< avgwordsize;
}


CBSE Class 12 - Computer Science - C++ Practical Text File Processing - Snippet-8 (#cbseNotes)

No comments:

Post a Comment