Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(8 points) Complete the code for the following replaceinFile function. Its purpo

ID: 3908880 • Letter: #

Question

(8 points) Complete the code for the following replaceinFile function. Its purpose is to copy new file. It uses the find function and replace function from the string library 1. an existing file, replacing all occurrences of one string with another to the // Returns index where str2 is found in strl, or -1 strl.find (string str2. int pos) // Replaces n chars in strl. starting at pos, with str2 strl.replace (int pos, int n, string str2) The main program will prompt the user for the info prior to calling the function: Select visual studio 201 nter your old filenane: cmp.txt ter your new filename: cVhotdog.txt ter your new string: candy nter your old string: cow replacements have been made and stored in the new file ss any key to continue void replaceInFile(string oldfile, string newfile, string oldstr, string newstr) ifstream infile; infile.open ( ofstream outfile; outfile.open ( string line; ) //open input file //open output file // loop until the input file variable becomes null while (infile) // get a line from the input file getline( if (infile) // if not end of file , line) ; for (int i 0; i

Explanation / Answer

void replaceInFile(string oldfile, string newfile, string oldstr, string newstr)

{

ifstream infile;

infile.open(_________oldfile_________); //open input file

ofstream outfile;

outfile.open(________newfile__________); //open output file

string line;

while (infile) // loop until the input file variable becomes null

{

getline(___infile________, line); // get a line from the input file

if (infile) // if not end of file

{

for (int i = 0; i < line.length()__________; i++)

{

int position = line.find(___oldstr________, i);

if (position >= 0)

{

line.replace(position, oldstr.length()___________,

___newstr_______);

}

}

outfile << ___line_______ << " ";

}

}

infile.close()__________;

outfile.close()________;

return;

}