For this question I am struggling to figure out how to insert the character into the starting position. Within the code I put my guess as to what it might be or along those lines, but I know that you cannot use string operations with a char. Please if anybody could explain how to solve this, i would greatly appreciate it.
Given a string, an integer position, and a character, all on separate lines, find the character of the string in that position and replace it with the character read. Then, output the result.
Ex: If the input is:
warn
0
e
the output is:
earn
Note: Using a pre-defined string function, the solution can be just one line of code.
#include <iostream>
#include <string>
using namespace std;
int main() {
string strVal;
int strPosition;
char charValue;
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
For this question I am struggling to figure out how to insert the character into the starting position.
There is the 1st problem. You say "insert" when your example looks like "replace".
The next problem is that most people would start counting at (1) not (0)zero as it needs to be. This could put your position past the end of the string.
#include <iostream>
#include <string>
usingnamespace std; // <--- Best not to use.
int main()
{
string strVal; //<--- Empty when defined. Does not need initialized.
char charToChange{}; // <--- ALWAYS initialize all your variables.
std::string charValue{ "*" };
std::cout << "\n Enter a string: "; // <--- The "cin" ALWAYS needs a prompt.
getline(cin, strVal);
//cin >> strPosition;
std::cout << " Enter a character to change: ";
cin >> charToChange;
//Your code goes here
//my guess
strVal.replace((strVal.find(charToChange)), 1, charValue);
cout << "\n " << strVal << '\n';
return 0; // <--- Not required, but makes a good break point for testing.
}
But what about position that is an input requirement?
If the char to change is not found, then .find() will return std::string::npos which is greater than the string length, so .replace() will then throw out_of_range. So doing it like this requires a try/catch.
I personally reserve try/catch for errors beyond my control.
so I would split the find from the replace into 2 statements and only replace if you found it.
but it works both ways, its just a style thing.
Just to be clear to mattman305, we've gone away from the original problem. As salem C and seeplus pointed out, the original question can be solved with a one-liner: str[pos] = ch;. We're now discussing a different problem, where you're given the character to search for, not the position.
... assuming that pos is valid for str. If the value of pos cannot be guaranteed to be valid, then either a test before [] or .at() and try/catch are required.
to insert the character into the starting position
insert - as opposed to replace - at the specified position:
strVal.insert(strPosition, 1, charValue);
with the same caveat re strPosition being valid or first tested or try/catch used.