String Operations

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;

getline(cin, strVal);
cin >> strPosition;
cin >> charValue;

/* Your code goes here */
/* my guess
strVal.insert(strPosition,charValue);
*/
cout << strVal << endl;

return 0;
}
There's no need to find the char in the string as you are given the position. As salem c mentions above, just use [].

Or is there more to this question as this is trvial ??????
Hello mattman305,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

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.

This should give some ideas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace 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.
}


Andy
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.
try/catch is expensive if triggered. However, it removes the extra test which is done by the .replace() code anyway.

Which way would probably depend upon its usage and the ratio of found/not found. Also how many times this is called and the context.

All things being equal, I'd probably go for replace only if found:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
	std::string strVal;
	char charToChange {};
	char newChar {};

	std::cout << "Enter a string: ";
	std::getline(std::cin, strVal);

	std::cout << "Enter a character to change: ";
	std::cin >> charToChange;

	std::cout << "Enter replacement char: ";
	std::cin >> newChar;

	if (const auto fnd {strVal.find(charToChange)}; fnd != std::string::npos)
		strVal.replace(fnd, 1, 1, newChar);

	std::cout << '\n' << strVal << '\n';
}

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.
Last edited on
Topic archived. No new replies allowed.