Respuesta :
Answer:
#include <iostream>
using namespace std;
const string replace(const string workStr, int idx, const char newChar) {
string ans = "";
for (int i = 0; i < workStr.length(); ++i)
ans += (i != idx ? workStr[i] : newChar);
return ans;
}
int main() {
string workStr;
int idx;
char newChar;
getline(cin, workStr);
cin >> idx;
cin >> newChar;
cout << replace(workStr, idx, newChar) << endl;
return 0;
}
Explanation:
Declares the necessary variables. Next read them and finally call a replace user defined function to solve the problem.
The program is given in C++. So, the code must be completed in C++.
The missing part of the program can be completed with the following lines of code:
string newStr(1, newChar);
workStr.replace(idx,1,newStr);
To complete the code, we first convert newChar to a string.
This is done using:
string newStr(1, newChar);
In the above code segment, a string variable newStr is initialized with the value of newChar
Next, replace the character of the string using:
workStr.replace(idx,1,newStr);
The syntax of the above instruction is:
string-variable.replace(position, number of characters to replace, replacement string)
In this case,
- The string variable is workStr
- The position of string to replace is idx
- The number of characters is 1 (because only one character is to be replaced)
- The replacement string is newStr
See attachment for the complete program and a sample run
Read more about C++ programs at:
https://brainly.com/question/24494143
