Hello, I am trying to write a program that ask the user to enter a paragraph and then a word to be searched for in the paragraph. The program must then search the word in the paragraph in a loop and continue to output each place the word is found. I am having trouble with the formula for the find. How would I write the program to start searching after last occurence of the word found?
#include <iostream>
#include <string> // working with strings -- strings are objects with their own member functions
usingnamespace std;
char paragraph;
int main()
{
string paragraph;
cout << "Please enter your paragraph:";
getline (cin,paragraph);
cout << "Hello, your paragraph is " << paragraph << "!\n";
string paragraph1 = paragraph;
//string sentence = paragraph + " " +paragraph + " " + paragraph;
//cout << sentence << endl;
cout << "The size of your paragraph = " << paragraph.size() << " characters. \n\n";
string word;
cout << "Please enter the word you are searching for:";
getline (cin,word);
cout << "Hello, your word is " << word << "!\n";
bool wordsearch = true;
do {
if (paragraph.find(word) == string::npos)
cout << "" << word << " does not exist in the sentence" << endl;
//(s.begin(), s.end(), '_');
//for (int i = 0; i < paragraph.size(); ++i)
//cout << paragraph[i] << endl;
cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl << endl;
if (paragraph.empty())
{
cout << "\nThe sentence is empty" << endl;
}
else
cout << "\nThe sentence is not empty" << endl;
system ("pause");
}while (wordsearch = true);
}
How would I write the program to start searching after last occurence of the word found?
Most find functions/algorithms take an extra optional parameter which is the starting index to start searching at. Generally it is defaulted to 0, but you can give it a value after the most recently found occurrence to have it find a later one.
Thank you for your help. I can do that and it would work fine if I knew in advance how many times the word is in the phrase but the user is choosing the phrase. How would I make the program skip to the most recently found occurrence?
#include <iostream>
#include <string> // working with strings -- strings are objects with their own member functions
usingnamespace std;
char paragraph;
int main()
{
string paragraph;
cout << "Please enter your paragraph:";
getline (cin,paragraph);
cout << "Hello, your paragraph is " << paragraph << "!\n";
string paragraph1 = paragraph;
cout << sentence << endl;
cout << "The size of your paragraph = " << paragraph.size() << " characters. \n\n";
string word;
cout << "Please enter the word you are searching for:";
getline (cin,word);
cout << "Hello, your word is " << word << "!\n";
size_t found = paragraph.find(word);
bool wordsearch = true;
while (paragraph.find(word) != string::npos) {
if (paragraph.find(word) == string::npos)
cout << "" << word << " does not exist in the sentence" << endl;
std::size_t found = paragraph.find(word);
if (found!=std::string::npos)
std::cout << "first 'word' found at: " << found << '\n';
found=paragraph.find((word),found+1);
if (found!=std::string::npos)
std::cout << "second 'word' found at: " << found << '\n';
found=paragraph.find((word),found+1);
if (found!=std::string::npos)
std::cout << "third 'word' found at: " << found << '\n';
found=paragraph.find("word");
if (found!=std::string::npos)
std::cout << "'word' also found at: " << found << '\n';
found=paragraph.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
system ("pause");
}while (wordsearch = true);
}