I need help with this C++ problem. Please Get a line of text from the user. Outp
ID: 3663877 • Letter: I
Question
I need help with this C++ problem. Please Get a line of text from the user. Output that line. Then output the line again, this time expanding all of the found common text message abbreviations. You can do this by using a loop. Ex: Enter text: My BFF is David. IDK who your BFF is. You entered: My BFF is David. IDK who your BFF is. Expanded: My best friend forever is David. I don't know who your best friend forever is. Support these abbreviations: BFF -- best friend forever IDK -- I don't know JK -- just kidding TMI -- too much information TTYL -- talk to you later Note: If an abbreviation appears more than once, every instance gets expanded.
Explanation / Answer
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<iterator>
#include<map>
using namespace std;
map<string, string> myMap;
int contains(string str) {
map<string,string>::iterator it = myMap.find(str);
if(it != myMap.end())
{
return 1;
}
return 0;
}
int main() {
myMap["BFF"] = "best friend forever";
myMap["IDK"] = "I don't know";
myMap["JK"] = "just kidding";
myMap["TMI"] = "too much information";
myMap["TTYL"] = "talk to you later";
string text;
cout<<"Enter text: ";
getline(cin,text);
cout<<"You entered: "<<text<<endl;
cout<<"Expanded: ";
stringstream ss(text);
string item;
while (getline(ss, item, ' ')) {
if(contains(item) == 1){
cout <<myMap[item]<<" ";
}else{
cout<<item<<" ";
}
}
}