Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the replace(string, const string, const string) used in the following

ID: 3543211 • Letter: I

Question

Implement the replace(string, const string, const string) used in the following code. You will need to look up the functionality of the std::string object found in the string.h library.

Here is an example of correct output/behavior from the program:

> Hello new employee!
> What is your first name? Bob
> What is your last name? Lambert
>
> Thank You!
> Here is your name tag:
> * EMPLOYEE:
> * Lambert,
> * Bob

(*HINT* Use the internet, specifically cplusplus.com)

/* Name: xxxxxxx
* Id: xxxxxxx
* Course: xxxxxxx
* Project: xxxxxxx
* Date: xxxxxxx
*
* Will ask a new employee what their first and last names are,
* modifying their employee tag with the new values.
* It will finally output the name tag to the screen.
*/
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string);

int main() {

// Default Employee name on tag
const string DEFAULT_NAME_LAST("{DEFAULT_NAME_LAST}");
const string DEFAULT_NAME_FIRST("{DEFAULT_NAME_FIRST}");

// to hold string input from employee
string buffer("");

// Employee's ID Tag
string employee_tag("* EMPLOYEE: ");
// add employee's last name first
employee_tag.append("* " + DEFAULT_NAME_LAST + ", ");
employee_tag.append("* " + DEFAULT_NAME_FIRST + " ");

// ask the employee for their first name
cout << "Hello new employee!" << endl;
cout << "What is your first name? ";
cin >> buffer;

// replace their first name with string in buffer
replace(employee_tag, DEFAULT_NAME_FIRST, buffer);
// then clear the buffer
buffer.clear();

// adk the employee for their last name
cout << "What is your last name? ";
cin >> buffer;
cout << endl;

// replace their last name with string in buffer
replace(employee_tag, DEFAULT_NAME_LAST, buffer);
// then clear the buffer
buffer.clear();

// thank them and display their name tag:
cout << "Thank You!" << endl;
cout << "Here is your name tag: " << endl;
cout << employee_tag;

exit(EXIT_SUCCESS);
}

/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string) {

// find position of sub_string in user_string

// replace sub_string in user_string with new_sub_string
}

Explanation / Answer

/* Name: xxxxxxx
* Id: xxxxxxx
* Course: xxxxxxx
* Project: xxxxxxx
* Date: xxxxxxx
*
* Will ask a new employee what their first and last names are,
* modifying their employee tag with the new values.
* It will finally output the name tag to the screen.
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string);
int main() {
// Default Employee name on tag
const string DEFAULT_NAME_LAST("{DEFAULT_NAME_LAST}");
const string DEFAULT_NAME_FIRST("{DEFAULT_NAME_FIRST}");
// to hold string input from employee
string buffer("");
// Employee's ID Tag
string employee_tag("* EMPLOYEE: ");
// add employee's last name first
employee_tag.append("* " + DEFAULT_NAME_LAST + ", ");
employee_tag.append("* " + DEFAULT_NAME_FIRST + " ");
// ask the employee for their first name
cout << "Hello new employee!" << endl;
cout << "What is your first name? ";
cin >> buffer;
// replace their first name with string in buffer
replace(employee_tag, DEFAULT_NAME_FIRST, buffer);
// then clear the buffer
buffer.clear();
// adk the employee for their last name
cout << "What is your last name? ";
cin >> buffer;
cout << endl;
// replace their last name with string in buffer
replace(employee_tag, DEFAULT_NAME_LAST, buffer);
// then clear the buffer
buffer.clear();
// thank them and display their name tag:
cout << "Thank You!" << endl;
cout << "Here is your name tag: " << endl;
cout << employee_tag;
exit(EXIT_SUCCESS);
}
/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string, const string& sub_string, const string& new_sub_string)
{
// find position of sub_string in user_string
// replace sub_string in user_string with new_sub_string
int position = user_string.find(sub_string);
user_string.replace(position,sub_string.length(),new_sub_string);
}