Trouble with Find and Replace

I am having trouble getting the words to print before lazy(line 22) and getting the new sentence to print. Also can someone reference me to how if the word that you search is not found it will just print "(the word you searched for)" is not found.
Line 24 after ":" is where the new sentence is suppose to go

The sentence: the quick brown fox jumped over the lazy red dog as he lay sleeping.

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
using namespace std;

//http://www.cplusplus.com/reference/string/string/getline/

//void notFound(string searchTerm)

  //  if(searchTerm)
        //cout << "The word " << searchTerm << " is not in your sentence" << endl;



int main()
{
    std::string enterSentence;
    std::cout << "Enter a sentence: ";
    std::getline(cin,enterSentence);

    std::string searchTerm;
    std::cout << "Enter a search term: ";
    std::getline(cin,searchTerm);
    int chrAmount=searchTerm.length();

    std::string replaceTerm;
    std::cout << "Enter the replacement term: ";
    std::getline(cin,replaceTerm);
    std::cout << endl;

    int termPosition = enterSentence.find(searchTerm);

    std::cout << "Your original sentence is: "<< enterSentence << endl;

    std::cout << "Found the word " << searchTerm << " at position: " << termPosition << endl;
    //notFound(searchTerm,enterSentence);

    std::cout << "  This word has a length of "<< chrAmount <<" chars." << endl;

    std::cout << "The parts before " << searchTerm << " are: " << enterSentence.substr(enterSentence.length()-68,termPosition) << endl;

    std::cout << "The parts after " << searchTerm <<" are: " << enterSentence.substr(enterSentence.length()-28,termPosition) << endl;

    std::cout << "Replacing " << searchTerm << " with " << replaceTerm << ": " << enterSentence.replace(enterSentence.find(searchTerm),searchTerm.length(),replaceTerm) <<endl;

    return 0;

}
Last edited on
You're using a function std::string::find.

Refer to it's documentation to use it properly.
Topic archived. No new replies allowed.