Question

In C++ write a program that will read three strings from the user, a phrase, a...

In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi".

in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter to be replaced, the third is the replacement letter, and the 4th is an empty and modifiable vector that must contain the new phrase with the replaced letters.

For the function replace, loop through all the characters in the phrase (1st parameter) and use the string function replace to perform each letter replacement where needed. Also, use push_back to insert the letters in the new phrase into the vector (last parameter).

For the functiondisplay_phrase(), the function replaced displays the contents of the vector passed in.

The code: the main method should not need to be changed

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// Function prototypes
void replace(string & phrase, const string & letter, const string & replacement, vector<char> & text);
void display_phrase(const vector<char> & text);

int main()
{
string phrase, letter, replacement;
vector<char> text_vec;

cout << "Enter phrase: ";
getline(cin, phrase);

if (phrase.length() > 0)
{
   cout << "Enter letter to replace: ";
   getline(cin, letter);

   if (letter.length() == 1)
   {
       cout << "Enter replacement letter: ";
       getline(cin, replacement);
  
       if (replacement.length() == 1)
       {
           replace(phrase, letter, replacement, text_vec);
           cout << "String phrase: " << phrase << endl;
           cout << "Vector phrase: ";
           display_phrase(text_vec);
           cout << endl;
       }
       else
       {
           cout << "Replacement letter must be a single letter" << endl;
       }
   }
   else
   {
       cout << "Letter to replace must be a single letter" << endl;
   }
}
  
return 0;
}

// FUNCTION
void replace(string & phrase, const string & letter, const string & replacement, vector<char> & text)
{
   // Insert your code here
}

void display_phrase(const vector<char> & text)
{
   // Insert your code here
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// Function prototypes
void replace(string & phrase, const string & letter, const string & replacement, vector<char> & text);
void display_phrase(const vector<char> & text);

int main()
{
string phrase, letter, replacement;
vector<char> text_vec;

cout << "Enter phrase: ";
getline(cin, phrase);

if (phrase.length() > 0)
{
cout << "Enter letter to replace: ";
getline(cin, letter);

if (letter.length() == 1)
{
cout << "Enter replacement letter: ";
getline(cin, replacement);
  
if (replacement.length() == 1)
{
replace(phrase, letter, replacement, text_vec);
cout << "String phrase: " << phrase << endl;
cout << "Vector phrase: ";
display_phrase(text_vec);
cout << endl;
}
else
{
cout << "Replacement letter must be a single letter" << endl;
}
}
else
{
cout << "Letter to replace must be a single letter" << endl;
}
}
  
return 0;
}

// FUNCTION
void replace(string & phrase, const string & letter, const string & replacement, vector<char> & text)
{
// Insert your code here
vector<char> v(phrase.length());
text=v;
for(int i=0;i<phrase.length();i++)
{
text.push_back(phrase.at(i));
}
//cout<<text;
for(std::vector<int>::size_type it = 0; it != text.size(); it++)
{
if(text[it]==letter.at(0)) //Comparing both the characters
{
text[it]=replacement.at(0);
}
}
}

void display_phrase(const vector<char> & text)
{
// Insert your code here
for(std::vector<int>::size_type it = 0; it != text.size(); it++) {
cout<<text[it]; //Printing modified String which is in form of vector
}
}

Sample i/o:

Explanation:

A vector named v of length of the string has been created. The created vector has been assigned to the text. Now  size_type type of iteration is used and when ever there is a match with given character the replacement character is given. A sample input output was given for better understanding

Add a comment
Know the answer?
Add Answer to:
In C++ write a program that will read three strings from the user, a phrase, a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • The code keeps saying its generating too much output check for any unterminated loops. I cant...

    The code keeps saying its generating too much output check for any unterminated loops. I cant find any. Can anyone help? #include <iostream> #include <string> using namespace std; char printMenu(); int GetNumOfNonWSCharacters(string); int GetNumOfWords(string); void ReplaceExclamation(string&); void ShortenSpace(string&); int FindText(string, string); int main(){ char option; string text, phraseToFind; cout << "\n\n Enter a sample text: "; getline(cin, text); cout << "\n\n You entered: " << text; while(1){ option = printMenu(); switch(option){ case 'q': case 'Q': return 0; case 'c': case...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT