Saturday 12 August 2017

C++ Practical Text File Processing Snippet-9 - Count A Particular Word (#cbseNotes)

C++ Practical
Text File Processing Snippet-9
Count A Particular Word 

C++ Practical Text File Processing Snippet-9 - Count A Particular Word (#cbseNotes)

Question :  Write a function in C++ to print the count of the word "is" as an independent word in a text file DIALOG.TXT
e.g. if the content of the file is:
      "This is his book. Is this book good?"
then the output of the program should be 2

Answer:




// Question: Write a function in C++ to print the count of
// the word "is" as an
// independent word in a text file DIALOG.TXT
// e.g. if the content of the file is:
//   This is his book. Is this book good?
// then the output of the program should be 2

#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
#include <string.h>

void countword();

void countword()
{
 ifstream fin;
 fin.open("DIALOG.TXT");
 char word[30];
 int count=0;
 while(!fin.eof())
 {
  fin>>word;
  if(strcmpi(word,"is") == 0)
   count++;
 }
 cout<<"Number of the word \"is\" in file are "<<count;
 fin.close();
}
int main()
{
 countword();
 return 0;
}


C++ Practical Text File Processing Snippet-9 - Count A Particular Word (#cbseNotes)

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.