Hi ,
i have a text file that contain a story
How do i extract the out all the sentences that contain the word Mon. in C++
I only want to show those sentences that contain the word mon
eg.
Monkey on a tree.
Rabbit jumping around the tree.
I am very rich, I have lots of money.
Today is monday.
tomorrow is tuesday.
but only those words that contain the word mon is show.
Monkey on a tree
<Press enter to continue or q to quit>
I have lots of money
<Press enter to continue or q to quit>
Today is monday
<Press enter to continue or q to quit>
any advices on that
below is my codes, but it come out as a chunk of word that i search for.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
char filename[50];
string searchword;
string s;
int count;
cout << "Enter filename (including .txt): ";
cin >> filename;
fin.open(filename);
// stores Search Word
string askforsearchword();
searchword = askforsearchword();
// ends program if no txt file is found
if (!fin.good())
{
cout << "File not found" << endl;
system("pause");
return 1;
}
while(!fin.eof())
{
// use getline to read entire line
getline(fin, s);
int i, position;
//converts text to all lowercase
for (i=0; i<s.length();i++)
s=tolower(s);
for (i=0; i<searchword.length();i++)
searchword=tolower(searchword);
position = s.find(searchword);
if (position != string :: npos)
{
cout << s << endl;
}
}
fin.close();
cout << "End Of Search." << endl;
system("pause");
return 0;
}
//function to ask for search word.
string askforsearchword()
{
string search;
cout<< "Type in your Search Word: " << endl;
cin >> search;
return search;
}