Write a code in a high-level language C++ for the automaton (given in the attach
ID: 3543061 • Letter: W
Question
Write a code in a high-level language C++ for the automaton (given in the attached PDF file) which accepts keywords (cat,bat,cab). Create an input file with these words (may be two or three copies of these words scattered in a paragraph and show that your program does accept these words and gives an output to that effect.. Now take a file which does not have these key words and show that your program gives an output that the input does not have any of these words.
Consider the set of keywords {cat, bat, cab}. We can use build keyword FSM to build to DFSM to accept strings that contain at least one of these keywords. We begin by creating a state and then a path to accept the first keyword, cat: Next we add branches for the remaining keywords, bat and cab: Finally, we add transitions that let the machine recover after a path dies:Explanation / Answer
#include <iostream> // std::cout
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> keys;
cout << "key words? " << endl;
for (;;)
{
string key;
if (!getline(cin,key) || key.empty())break;
keys.push_back(key);
}
string str; // Temp string to
cout << "Read from a file!" << endl;
vector<int> freq;
int c;
ifstream fin("thisfile.txt"); // Open it up!
while (fin >> str) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
for(int i=0;i<keys.size();i++){
if(strcmp(str.c_str(),keys[i].c_str())==0) {freq[i]+=freq;c++;}
}
}
fin.close(); // Close that file!
if(c==0) {
cout<<"key words did not occur in file "
exit(0);}
cout<<"key words occured in file "
for (int i = 0; i < keys.size(); ++i)
std::cout << keys.at(i) << "occured "<<freq.at(i)<<"many times"<<endl; // Print so I can see this all worked!
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.