This requires use of C++. Create a program that reads in a word from the user an
ID: 3889336 • Letter: T
Question
This requires use of C++. Create a program that reads in a word from the user and counts the number of occurrences of that word in a file called words.txt. The following code inputs a string and then reads a file called code.cpp. It then outputs the number of times the string appears.
many
Sample Output:
6
| wordoccurrences Micresoft Visual+2010 Express Taals Windaw File Edit Virw Praject Debug selution Lcalorer 0× words.tst wordoccarrences.cpp x Global Sape) main Soluticn i1 proj /iwith help fron Chepg.con Hcoder Filcs //#1 nr 1 ude " stdafx-h.. #1ncludectostrean> Cutaut Sthow uutpui from: targetver.h Resource Hiles | #includ@cstring> #include Hord) if (inputard) file.clase); cout wordsExplanation / Answer
#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{
ifstream fin("my_data.txt"); //reads text file
int count=0;
char check[20],word[20];
cout<<"Enter a word to count:";
gets(word); // reads word given by us
while(fin)
{
fin>>check; // read strings from file
if(strcmp(check,word)==0) // performing string comparision
count++; // if matches count increases.
}
cout<<"Occurrence="<<count<<"n";
fin.close(); //closing file
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.