Question

Find the frequency of any given word in a book. Please solve the problem using c++

Find the frequency of any given word in a book. Please solve the problem using c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Book.txt

This C++ program will read a word from user and then count its total occurrence in a text file “book.txt”. Make sure you have already create this text file and have some text in it. Place this file in the same directory where your program source file is present.

=================================================

C++ Code:

// Find the frequency of any given word

#include <iostream>

#include <cstdlib>

#include<fstream>

#include<string.h>

int main()

{

  std::ifstream fin;

  fin.open("book.txt");

   //opening text file

int count=0;

char ch[20],c[20];

/* Input word from the user */

std::cout<<"Enter a word to count the frequency:";

std::cin >> c;

while(fin)

{

fin>>ch;

if(strcmp(ch,c)==0)

count++;

}

std::cout<<"Frequency of a word "<<c<<" is = "<<count;

fin.close(); //closing file

}

=========================================

Output:

Enter a word to count the frequency: file

Frequency of a word file is = 4

Output Snapshots:

Please give Thumbs up...

Add a comment
Know the answer?
Add Answer to:
Find the frequency of any given word in a book. Please solve the problem using c++
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT