Replace

I'm writing a code that replaces the word read with the word study. So for the first sentence the correct answer will be I'm studying. The assignment requires that the strings are const. And also that the it shouldn't matter if read is written with big or small letters. The problem that I have is that I get no output at all.
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
#include<iostream>
#include<string>


using namespace std;

int main ()
{
    
    
    
    string const inText1 = "I'm reading. ";
    string const inText2 = "I like to read. ";
    string const inText3 = "I'm gonna read that book. ";
    string const inText4 = "She's reading. ";
    string const inText5 = "He's reading. ";
    string const inText6 = "READ. ";
    string const inText7 = "Reading. ";
    
    string inText8=inText1+inText2+inText3+inText4+inText5+inText6+inText7;
    string::size_type pos = inText8.find("read");
    while(pos != string::npos)
        {
        inText8.replace(pos, 4, "study");
        }
    cout << inText8 << endl;
    
    
    return 0;
    
    
}
In the while loop, don't you need to change the value of pos to look for the next occurrence of the word you're trying to find? Otherwise it seems you have an infinite loop.
Hello markusfurst,

In the while condition I changed the != to < in line 22 and added pos = inText8.find("read"); after the line 24 replace statement and it worked.

Hope that helps,

Andy
Thank you Andy!
Topic archived. No new replies allowed.