10.6k views
3 votes
Jump to level 1

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.

I am looking for the code for C++ NOT Python. Thanks

1 Answer

5 votes

Answer:

Assuming the input string variable is named `input`, the input int variable is named `index`, and the input char variable is named `ch`:

```

input[index] = ch;

cout << input;

```

Step-by-step explanation:

In C++, you can change specific elements of a string without having to overwrite the entire string (i.e. in C++, strings are mutable).

User Tito Sanz
by
7.4k points