Question

(C++) a loop that when you search for a word from a txt file, it will...

(C++)

a loop that when you search for a word from a txt file, it will display every occurrence of the word

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

CODE:

#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main(){
//declaring fstream object file
fstream file;

//initializing the string variables
string word,filename,search;
int frequency = 0,count = 0;

//input text file
filename = "input.txt";

//opening the file
file.open(filename.c_str());

//asking the user to enter the word to be searched in the text file
cout<<"Enter the word to be searched in the text file:"<<endl;
cin>>search;

//loop to search the word
while(file>>word){
//keeping a count of the words
count += 1;
if(word == search){
//if the word is found

//frequency is increased
frequency += 1;
//the number of words after which the searched word was found is displayed
cout<<"The word '"<<search<<"' found at word index: "<<count<<endl;
}
}

//the frequency of the word is printed in the console
cout<<"The frequency of the word '"<<search<<"' in the text file is: "<<frequency<<endl;

return 0;
}

____________________________________________________

CODE IMAGE:

_________________________________________

OUTPUT:

________________________________________________

INPUT TEXT FILE:

This is a text file which will be read by a C++ program which will find every occurences of a word and print its frequency

__________________________________________________________________

Feel free to ask any questions in the comments section

Thank You!

Add a comment
Know the answer?
Add Answer to:
(C++) a loop that when you search for a word from a txt file, it will...
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
  • C++ not C please Lyric Search Write a program that asks the user for a file...

    C++ not C please Lyric Search Write a program that asks the user for a file name and a string to search for. The program should search the file for every occurrance of the string. When the string is found it should display the line that it iwas found on and report the total number of occurrances of the string at the end. Place a text file with your favorite song lyrics in it and use to to perform a...

  • create a program that sum value from a txt file in c++,the data in the txt...

    create a program that sum value from a txt file in c++,the data in the txt file are in columns

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...

    Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and american2.txt). Each file will have words in random order. The output must be a file called words.txt. Note that you cannot cheat by using Linux commands to do this. It must be done entirely in your C code. File format: apple banana pear . . . Hint: Program will need to utilize double pointers. More Hints: 1. Assume no word is bigger that 50...

  • out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs...

    out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs to be handled and is specifically the part I am having trouble with https://www.dropbox.com/s/ume3slnphyhi6wt/out1.txt?dl=0 - link for the out1.txt file for testing yourself Please include comments as I am in this for learning and would really appreciate the help! Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start...

  • Using RStudio, Find a text file (.txt file) on your own. Create a word cloud. Submit...

    Using RStudio, Find a text file (.txt file) on your own. Create a word cloud. Submit the file, your code and the result.

  • In python, how to sort the 10 highest word length from a txt file displayed in...

    In python, how to sort the 10 highest word length from a txt file displayed in an ascending order? Word list: pizza toy phone computer chair desk shelf cup bed window clock house roof pen example: Enter name of files on at a time. when ready to compile, enter 'done': done longest words! responsibilities: 16 letters interdependence: 15 letters disillusionment: 15 letters administrations: 15 letters transportation: 14 letters restrictionism: 14 letters neocolonialism: 14 letters multiplication: 14 letters industrialized: 14 letters...

  • Using regex, write a linux expression to search up the following in a .txt file to...

    Using regex, write a linux expression to search up the following in a .txt file to match any IP address, the answer will show up in red: Answer: 192.168.1.111 255.255.255.255 1.12.123.0 0.0.0.0 .txt file: 192.168.1.111 255.255.255.255 10.abc.1.13 192.168.1 1.12.123.0 18.19.20 255.255.255. 19211111111 .123.123. ... 0.0.0.0

  • Language is C++ I am reading in data from a file. In the .txt file is...

    Language is C++ I am reading in data from a file. In the .txt file is a series of dates. I need help converting dates from the format "20-Apr-18" to the standard "4/20/18" I've gotten the rest taken care of, I just need help converting the dates from the file. Thank you.

  • 13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...

    13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exist then the definition will display. You can also add words to the dictionary. For testing you...

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
ADVERTISEMENT