In: Computer Science
C++
Text message decoder
Use getline() to get a line of user input into a string:
Enter text: IDK if I'll go. It's my BFF's birthday.
Search the string using find() for common abbreviations and replace() them with their long form. In addition, use a for loop to iterate through each character of the string and replace any occurences of '.' with '!':
Enter text: IDK if I'll go. It's my BFF's birthday. I don't know if I'll go! It's my best friend forever's birthday!
Use loops for each abbreviation to replace multiple occurrences:
Enter text: Hi BFF. IDK where I'm going but it's not going to be with you! JK you know you're my BFF. IDK why I just said that. Anyway, TMI. Why do I always give people TMI? IDK. Alright TTYL bye BFF! Hi best friend forever! I don't know where I'm going but it's not going to be with you! just kidding you know you're my best friend forever! I don't know why I just said that! Anyway, too much information! Why do I always give people too much information! I don't know! Alright talk to you later bye best friend forever!
Support these abbreviations:
Use the predefined constants for each abbreviation BFF, IDK, etc. and each long form BFF_LONG, IDK_LONG, etc. rather than hardcoding strings inside the loop.
// do comment if any problem arises
//code
#include <iostream>
#include <string>
using namespace std;
// SHORT
const string BFF = "BFF";
const string IDK = "IDK";
const string JK = "JK";
const string TMI = "TMI";
const string TTYL = "TTYL";
// LONG
const string BFF_LONG = "best friend forever";
const string IDK_LONG = "I don't know";
const string JK_LONG = "just kidding";
const string TMI_LONG = "too much information";
const string TTYL_LONG = "talk to you later";
int main()
{
cout << "Enter text:\n";
string input;
getline(cin, input);
// run till there are no abreviations left
while (true)
{
// for BBF
if (input.find(BFF) != string::npos)
{
input.replace(input.find(BFF), 3, BFF_LONG);
}
// for JDK
else if (input.find(IDK) != string::npos)
{
input.replace(input.find(IDK), 3, IDK_LONG);
}
// for JK
else if (input.find(JK) != string::npos)
{
input.replace(input.find(JK), 2, JK_LONG);
}
// for TMI
else if (input.find(TMI) != string::npos)
{
input.replace(input.find(TMI), 3, TMI_LONG);
}
// for TTYL
else if (input.find(TTYL) != string::npos)
{
input.replace(input.find(TTYL), 4, TTYL_LONG);
}
// if none left then break
else
break;
}
// print final string
cout << endl
<< input;
}
Output: