Help with string replace lab

Hello all,
I am trying to write a code that takes a user input, makes it into a string and replace the that string with other input. I have tried substring and it doesn't seem to work. Here's what the problem says:
(1) Get a line of text from the user. Output that line.
Ex:
Enter text: IDK how that happened. TTYL.
You entered: IDK how that happened. TTYL.
(2) Output the line again, this time expanding common text message abbreviations.
Ex:
Enter text: IDK how that happened. TTYL.
You entered: IDK how that happened. TTYL.
Expanded: I don't know how that happened. talk to you later.
Support these abbreviations:

BFF -- best friend forever
IDK -- I don't know
JK -- just kidding
TMI -- too much information
TTYL -- talk to you later

I have been able to do the first part of the problem. However for the second part when I have used the string.replace function I keep on getting errors. The code I have in here is what I have figured out so far. I would like to get help in understanding what I am doing wrong please.
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
  #include <iostream>
#include <string>
using namespace std;

int main() {

  string input;

cout << "Enter text: ";
getline(cin, input);

cout << endl <<"You entered: " << input << endl;

if (input.find("BFF")!=string::npos)
{
//cout << "BFF: best friend forever" << endl;
input.replace(9, 1, " my best friend forever "); 
} 
   
if (input.find("IDK" )!=string::npos)
   {
   //cout << "IDK: I don't know" << endl;
   input.replace(9, 1, " I don't know");
   }

if (input.find("JK")!=string::npos)
{
  // cout << "JK: just kidding" << endl;
  input.replace(9, 1, "just kidding");
  }

if(input.find("TMI")!=string::npos)
{
 //cout << "TMI: too much information" << endl;
 input.replace(9, 1, "too much information");
}

if(input.find("TTYL")!=string::npos)
{
//cout << "TTYL: talk to you later" << endl;
input.replace(9, 1, "talk to you later");
}
 
//replace(indx, num, subStr) 
   cout << "Expanded: " + input << endl;
   
   return 0;
}
1
2
3
4
5
6
7
8
9
10
std::string replace( std::string text, std::string word, std::string replacement )
{
    const auto pos = text.find(word) ;

    if( pos != std::string::npos )
        return text.substr( 0, pos ) + replacement + text.substr( pos+word.size() ) ;
        //      part before the word + replacement + part after the word

    else return text ; // not found, return text unchanged
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

int main() {
    
    string input = "If IDK I'll tell you";
    cout << "You entered: " << input << endl;
    
    string search_string = "IDK";
    
    size_t pos = input.find(search_string);
    
    if (pos != string::npos)
    {
        cout << "IDK encountered" << endl;
        input.replace(pos, search_string.length(), "I don't know");
        
        cout << input << '\n';
    }
    else
        cout << "Search string not found\n";
    
    return 0;
}
closed account (48T7M4Gy)
Or you can go a step further and automate the process. As it stands though only the first code of each type is encountered.

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
#include <string>

using namespace std;

int main() {
    
    string code[]
    {
        "BFF","IDK","JK","TMI","TTYL"
    };
    size_t no_of_codes = sizeof(code)/sizeof(string);
    
    string meaning[]
    {
        "best friend forever","I don't know","just kidding",
        "too much information","talk to you later"
    };
    
    string input =
    "Sorry, that was TMI because I was JK because if IDK I'll tell you, BFF, TTYL";
    cout << "You entered: " << input << endl;
    
    for(size_t i = 0; i < no_of_codes; ++i)
    {
        string search_string = code[i];
        size_t pos = input.find(search_string);
        
        if (pos != string::npos)
        {
            input.replace(pos, search_string.length(), meaning[i]);
        }
    }
    cout << "FINAL: " << input << '\n';
    
    return 0;
}
closed account (48T7M4Gy)
Works even better with #include <iostream>
Topic archived. No new replies allowed.