Given string inputString on one line, character newLetter on a second line, and integer strIndex on a third line, change the character at index strIndex of inputString to newLetter
#include <iostream>
#include <string>
using namespace std;
int main() {
string inputString = "Information";
string newLetter;
int strIndex;
string ch;
cout<<"String: "<<inputString<<endl;
int l = inputString.size();
cout<<"Enter newLetter: ";
cin>>ch;
cout<<"Enter index <"<<l<<": ";
cin>>strIndex;
if(strIndex<=l) {
inputString.replace(strIndex,strIndex+1,ch);
cout<<"New String: "<<inputString<<endl;
}
return 0;
}
Comments
Leave a comment