1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;
int main()
{
ifstream in_file;
string fileName, text, words, test;
stringstream buffer;
int counter = 0;
cin >> fileName;
in_file.open(fileName.c_str());
if(in_file.fail())
{
cout << "Can't open file" << endl;
exit(1);
}
while(!in_file.eof())
{
in_file >> text;
}
in_file.close();
text.erase(std::remove(text.begin(), text.end(), '.'), text.end());
text.erase(std::remove(text.begin(), text.end(), ','), text.end());
text.erase(std::remove(text.begin(), text.end(), '!'), text.end());
text.erase(std::remove(text.begin(), text.end(), '?'), text.end());
text.find_last_of(" ");
in_file.open(fileName.c_str());
while(!in_file.eof())
{
buffer << in_file.rdbuf();
test = buffer.str();
getline(in_file, words);
}
in_file.close();
size_t pos = 0;
while(true)
{
pos = test.find(text, pos++);
if (pos != std::string::npos)
{
counter++;
}
else break;
}
cout << text << " " << counter;
return 0;
}
|