C++ Practical
Text File Processing Snippet-8
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; }
No comments:
Post a Comment
We love to hear your thoughts about this post!
Note: only a member of this blog may post a comment.