Replacement Strings

I'm having issues trying to find out which string function to use to replace the output after enter first and last name.

#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: \n");
// add employee's last name first
employee_tag.append("* " + DEFAULT_NAME_LAST + ",\n");
employee_tag.append("* " + DEFAULT_NAME_FIRST + "\n");

// 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
}the code you need help with here.
[/code]
What do you mean by "replace the output"?
When the program is running. The it will ask the user for the "first name". After user enters first name...it will ask for last name. After that will say welcome employee "DEFAULT FIRST NAME" and then "DEFAULT LAST NAME" the object of the assignment is to use replace.string to replace the deafults with the actual user's input.

Example First name: ABE
Last Name:LINCOLN


Output: Welcome ABE LINCOLN......new employee tag

Google is your friend:
http://stackoverflow.com/a/3418285/1959975

If you need help applying it to your program, feel free to ask.
I'm actually very interested on finding out how to apply it to this particular code.
Can you say specifically what aspect of it you're confused about?
I'm very, very new to c++, so the whole replace function honestly, in regards to that code.
That code is supposed to be done without changing anything in the main. I know that the buffer(the user input) is passed to the new_sub_string. I know you're supposed to find the sub_string in the user_string, the sub_string is the default_name_first/last, and the user_string is the employee tag. Then put the buffer given string in the employee tag...I know I need to find where default name is in the employee tag, but I don't understand how to replace it with the buffer.
this is what i come up with but only one error shows

const string DEFAULT_NAME_LAST(DEFAULT_NAME_LAST)
DEFAULT_NAME_LAST.replace(2,16, "")
const string DEFAULT_NAME_FIRST(DEFAULT_NAME_FIRST)
DEFAULT_NAME_FIRST.replace(2,17, "")



the .replace is causing it not to compile...HELP?
It looks like you are trying to modify the wrong string, and in the wrong part of the code.

From a purely technical point of view, the replace will fail because the string which you attempt to modify is declared as const which means you are not permitted to change them, by design.

However, you are supposed to be adding your code inside this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* 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
}

Notice there are three parameters, user_string, sub_string and new_sub_string

Two of those are declared as const, so obviously you are not supposed to change them.

You need to write code which finds sub_string inside user_string.

Then you need to replace that with new_sub_string
Last edited on
I think I get what you are saying......but i have tried everything finding which what to use.....I had demo wednesday and his demo got screwed up so badly. I am badly confused.

I don't know which one to use :/. I am extermely fustrated and confused....

is there a example you can show me or tell me in english what needs to be done. I appriciete all the help you guys are giving me.
Well, first you need to do the find() in order to know at which position in the string the text is to be replaced.

size_t pos = user_string.find(sub_string);

After that, you should ideally check whether the item was actually found
If it wasn't found, pos will be equal to string::npos

See reference:
http://www.cplusplus.com/reference/string/string/find/

Then you do the replace, the function needs three parameters, see the examples on the reference page below:
http://www.cplusplus.com/reference/string/string/replace/
so this is what I have for the replace function

void replace(string& user_string,
const string& sub_string,
const string& new_sub_string) {
int pos;
int size;
string str;

// find position of sub_string in user_string

pos = user_string.find(sub_string);

size = sub_string.size();

str = user_string;

// replace sub_string in user_string with new_sub_string

str.replace(pos, size , new_sub_string);

}


It compiles, but it wont change anything in the employee tag. I gave the replace function the starting position, the size of the text I want to change, and the text. I have no clue what I'm doing wrong...
That is looking very close to a correct solution.

The one thing you need to change is this, get rid of the local variable string str, it isn't needed.

Instead, apply the replace() directly on the parameter user_string. That should fix the problem.
Can you explain why exactly I wouldn't need string str? How would I apply the replace() to the user_string parameter?
Can you explain why exactly I wouldn't need string str?
You don't need it because it is a local variable which exists only for the duration of the function. When the function ends, the variable is destroyed and any changes you made to it are lost.

How would I apply the replace() to the user_string parameter?
change this line:
 
    str.replace(pos, size , new_sub_string); 

to this:
 
    user_string.replace(pos, size , new_sub_string); 

UNO students. Pfft
Last edited on
Topic archived. No new replies allowed.