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 only the first sentence is changed. not the others.
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
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>

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");
    inText8.replace(pos, 4, "study");
    cout << inText8 << endl;
    
    


    
    return 0;
    
    
}
Last edited on
You need a loop that repeats until pos is string::npos.

http://www.cplusplus.com/reference/string/string/npos/
Something like this? I get the same results though.

1
2
3
4
5
 string inText8=inText1+inText2+inText3+inText4+inText5+inText6+inText7;
 string::size_type spacePos = inText8.find("read");
 if(spacePos != string::npos)
     inText8.replace(spacePos, 4, "study");
 cout << inText8 << endl;
Something like this?

Key word: loop - you don't have one here.
Tried with while and for aswell but it didn't work.
Topic archived. No new replies allowed.