Question

In: Computer Science

C++ Text message decoder Use getline() to get a line of user input into a string:...

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:

  • BFF -- best friend forever
  • IDK -- I don't know
  • JK -- just kidding
  • TMI -- too much information
  • TTYL -- talk to you later

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.

Solutions

Expert Solution

// 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:


Related Solutions

3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of...
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of user input into a string. Output the line. (3 pts) Ex: Enter text: IDK if I'll go. It's my BFF's birthday. You entered: IDK if I'll go. It's my BFF's birthday. (2) Search the string (using find()) for common abbreviations and print a list of each found abbreviation along with its decoded meaning. (3 pts) Ex: Enter text: IDK if I'll go. It's my...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could only include alphabetic letters and space. There are 26 alphabetic letters. Consider space the 27th letter. The user then enters a shift code that should be an integer between -26 and 26. The application will show the encoded/decoded message based on the shift code entered. If you encode a message, each letter in the message will be moved forward through the alphabet according to...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
A Java program that deciphers each message by building the string. Each line of the text...
A Java program that deciphers each message by building the string. Each line of the text file contains either a word or a command followed by a forward slash and the requirements of the command. A Stack Implementation is needed as. As you read the data from the text file, you need to know if it is a new phrase or command. The commands are: i – an insert followed by the letters, numbers, or symbols that need to be...
Write a program named StringWorks.java that asks the user to input a line of text from...
Write a program named StringWorks.java that asks the user to input a line of text from the keyboard.   Ask the user if they want their answers case sensitive or not. You output should be the list of words in the sentence including duplicates A sorted list of the words (alphabetically) A sorted list of words listed backwards (where z comes before a) A randomly shuffled list of works the list of words in the sentence alphabetically removing duplicates. You need...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Write a Python program to implement Vignere Cipher. Take user input to get plain text and...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
I want the linked list to be inserted by the user and donot use getline to...
I want the linked list to be inserted by the user and donot use getline to insert it #include <bits/stdc++.h> using namespace std;    // A linked list Node struct Node {     int data;     struct Node* next; };    // Size of linked list int size = 0;    // function to create and return a Node Node* getNode(int data) {     // allocating space     Node* newNode = new Node();        // inserting the required data     newNode->data = data;     newNode->next =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT