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;
}
|