#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
int Count= 0,
oldCount,
lineNo= 0,
pos;
string searchStr,
testStr,
testSearch,
fileName,
intStr;
cout << "File name? ";
getline(cin,fileName);
ifstream fin(fileName.c_str());;
if (!fin)
{
perror(fileName.c_str());
exit(1);
}
cout<<"Search string? ";
getline (cin, searchStr);
testSearch = searchStr;
for (int i=0; i<testSearch.size(); i++)
{
testSearch[i]=tolower(testSearch[i]);
}
while (getline (fin, intStr))
{
testStr=intStr;
for (int i=0; i=testStr.size(); i++)
{
testStr[i]= tolower(testStr[i]);
}
lineNo++;
oldCount=Count;
pos=0;
while((pos=testStr.find(testSearch, pos)) < testStr.size())
{
pos++;
Count;
}
if (Count>oldCount)
{
cout<<setw(5)<<lineNo<<": "<<intStr<<endl;
}
}
cout <<"There were" <<Count <<"occurances of "<<searchStr<<" in " <<fileName<<endl;
fin.close();
return 0;
}
djhog 0 Newbie Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.