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

Need to write the following function: char* replaceCopy(char* destination, const

ID: 3541606 • Letter: N

Question

Need to write the following function:


char* replaceCopy(char* destination, const char* source, char target, char replace)


Which needs to:

Copy a string in which each instance of a "target" character is replaced by the       "replace" character, from the address specified by source to that specified by destination. Return a pointer to the start of the copied string.


And the test portion in main looks like:

cout << "Replace 'd' in "Freddo" with 'y', should see "Freyyo". ";

   replaceCopy(name, names[2], 'd', 'y');

   cout << name << endl << endl;

Explanation / Answer

#include<iostream>

using namespace std;




char* replaceCopy(char* destination, const char* source, char target, char replace)

{

int i = 0;

while(source[i]!='')

{


if(source[i] == target)

destination[i] = replace;

else

destination[i] = source[i];

i++;

}destination[i] = source[i];

return destination;


}


int main()

{

char names[]="Freddo";

char name[100];

cout << "Replace 'd' in "Freddo" with 'y', should see "Freyyo". ";

replaceCopy(name, names, 'd', 'y');

cout << name << endl << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote