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 61 62 63 64
|
#include<fstream>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
ifstream inFile;
stringstream buffer;
string fileName;
string test;
string word;
string words;
int counter = 0;
cin >> fileName;
inFile.open(fileName.c_str());
if(inFile.fail())
{
return 0;
}
while(!inFile.eof())
{
inFile >> word;
}
inFile.close();
word.erase(std::remove(word.begin(), word.end(), '.'), word.end());
word.erase(std::remove(word.begin(), word.end(), ','), word.end());
word.erase(std::remove(word.begin(), word.end(), '!'), word.end());
word.erase(std::remove(word.begin(), word.end(), '?'), word.end());
word.find_last_of(" ");
inFile.open(fileName.c_str());
while(!inFile.eof())
{
buffer << inFile.rdbuf();
test = buffer.str();
getline(inFile, words);
}
inFile.close();
int unsigned pos = 0;
while(true)
{
pos = test.find(word, ++pos);
if (pos != std::string::npos)
{
counter++;
}
else break;
}
cout << word << ' ' << counter;
return 0;
}
|