-2

Hey I know this is a stupid question but I really need to complete an exercise in my programming class, and I can't find the solution anywhere.

Essentially I need to search a 'tweet' for abbreviations like lol and replace them with actual words.

I've tried this code but it deletes the beginning of the string sometimes.

string usertweet;
cout << "Please enter a tweet. " << endl; 
cin >> usertweet;

getline(cin, usertweet);
usertweet.resize(160); // Cut down the tweet to 160 chars.

int position; //Used for searching for abrvs.
position = usertweet.find("AFK");
if (position >= 0) {
    usertweet.replace(position,3,"Away from Keyboard");
}

position = usertweet.find("BRB");
if (position >= 0) {
    usertweet.replace(position,3,"Be Right Back");
}

cout << usertweet;

Sorry for the dumb question and thank you so much in advance!

Daymarquez
  • 329
  • 2
  • 3
  • Please give an example of input and output that isn't working properly. – chris Jul 08 '15 at 05:10
  • 1
    @MohitJain, Yes, and `position` doesn't currently reflect the type returned by `find`. If it did, the former check would always be true, which is risky if you ask me. – chris Jul 08 '15 at 05:14
  • @Adrian I honestly have spent several hours on this. I found what I felt to be an adequate solution, but as shown above it doesn't work. – Daymarquez Jul 09 '15 at 04:28

1 Answers1

0

Change to

string usertweet;
cout << "Please enter a tweet. " << endl; 
cin >> usertweet;

getline(cin, usertweet);
usertweet.resize(160); // Cut down the tweet to 160 chars.

for (size_t p = usertweet.find("AFK"); p != string::npos; p = usertweet.find("AFK", p + 1))
    usertweet.replace(p, 3, "Away from Keyboard");

for (size_t p = usertweet.find("BRB"); p != string::npos; p = usertweet.find("BRB", p + 1))
    usertweet.replace(p, 3, "Be Right Back");

cout << usertweet;
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50