Can't seem to find a way to get rid of the space at the end of my code. Help! Code is as follows:
#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";
myMap["IDK."] = "I don't know.";
myMap["TTYL."] = "talk to you later.";
myMap["BFF,"] = "best friend forever,";
myMap["JK,"] = "just kidding,";
myMap["JK."] = "just kidding.";
myMap["TMI!"] = "too much information!";
myMap["TMI."] = "too much information.";
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 << " ";
}
}
cout << endl;
}
Output would then result in:
Your best friend forever, my best friend forever, and her best friend forever are all here.
(followed by a space at the end of this sentence)
Last edited on