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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.