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 -D-D - Du 0 e the output is: earn Note: Using a pre-defined string function, the solution can be just one line of code. 1 #include 2 #include 3 using namespace std; 4 5 int main() { 6 string workStr; 7 int idx; 8 char newChar; 9 19 getline(cin, workStr); 11 cin >> idx; 12 cin >> newChar; 13 14 15 16 cout << workStr << endl; 17 18 return; 19} 1 2 3

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

Ver imagen MrRoyal
ACCESS MORE