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;
}
#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;
}
Comments
Leave a comment