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

make your own version of strcpy() called myStrcpy. Use the following template. N

ID: 3573884 • Letter: M

Question

make your own version of strcpy() called myStrcpy. Use the following template. Name your program strcpy.cpp.

#include <iostream>

using namespace std;

void myStrcpy(char dest[], const char source[]);

int main()

{

char str1[20] = "Hello CS111";
cout << str1 <<endl; //Hello CS111

myStrcpy(str1, "Oh PHYS302");

cout << str1 << endl; // Oh PHYS302

return 0;

}

void myStrcpy(char dest[], const char source[])

{

//Hint: I have only 4 lines of code inside this function

//Don’t forget to place ‘’ at the end.

}

Explanation / Answer

#include <iostream>
using namespace std;
void myStrcpy(char dest[], const char source[]);
int main()
{
char str1[20] = "Hello CS111";
cout << str1 <<endl; //Hello CS111
myStrcpy(str1, "Oh PHYS302");
cout << str1 << endl; // Oh PHYS302
return 0;
}
void myStrcpy(char dest[], const char source[])
{
//Hint: I have only 4 lines of code inside this function
//Don’t forget to place ‘’ at the end.
int i;
for(i=0; source[i]!=''; i++){
dest[i] = source[i];
}
}

Output:

sh-4.2$  g++ -o main *.cpp                                                                                                                                                                                                                             

sh-4.2$ main                                                                                                                                                                                                                                           

Hello CS111                                                                                                                                                                                                                                            

Oh PHYS3021