Answer to Question #345155 in C++ for Alvaro

Question #345155

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


1
Expert's answer
2022-05-26T09:14:38-0400
#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;
}

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