Searching for a word in a string

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?


Any suggestions?

Thank you for your help
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
  #include <iostream>
#include <string> // working with strings -- strings are objects with their own member functions

using namespace 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);
}
Radkowboy wrote:
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.
@LB

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?

Your help would be greatly appreciated.

This is what I have so far
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
#include <iostream>
#include <string> // working with strings -- strings are objects with their own member functions

using namespace 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);
}
Keep track of where the last found occurrence was and start searching after it - I thought I made this clear before, sorry.
Last edited on
Topic archived. No new replies allowed.