Answer to Question #237482 in C++ for Robi

Question #237482

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


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 stringVal;

  int stringPos;

  char substituteChar;

  

  getline(cin, stringVal);

  cin >> stringPos;

  cin >> substituteChar;


  


  cout << stringVal << endl;


  return 0;

}


1
Expert's answer
2021-09-15T02:29:16-0400
#include <iostream>
#include <string>
using namespace std;
int main() {


  string stringVal;


  int stringPos;


  char substituteChar;


   getline(cin, stringVal);


  cin >> stringPos;


  cin >> substituteChar;




  stringVal.at(stringPos)  =  substituteChar; //the solution done with one line of code as stated in the question
  
  cout << stringVal << endl;


  return 0;


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS