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

Need help with two C++ questions, please answer in basic understandable C++ code

ID: 3883418 • Letter: N

Question

Need help with two C++ questions, please answer in basic understandable C++ code language, comments would be greatly appreciated since it could allow me to better understand them. Thank you!

Greetings, our professor wants us to define char *strcopy( char *s1, const char *s2) and char *strcat(char *s1, const char *s2) in C++ language, he gave us an example of char *strlen here:

int mystrlen(const char pcString[])

{

int Length = 0;

while (pcString[Length] != '') Length++;

return Length;

}

However, he advised us to use pointer as a practice, and he posted another example below:

int mystrlen(const char *pcString)

/* Return the length of string pcString. */

{

const char *pcStringEnd = pcString;

while (*pcStringEnd != '') pcStringEnd++;

return pcStringEnd - pcString;

}

Both methods are fine (If you want to be extra helpful, you could write down both methods, that would be amazing for me to learn about it),please include comments! Thank you

Thank you for reading and helping!

*Sidenote: I doubt anyone here would need these, but our professor has told us the use of the function, just in case, I would post them below.

Definition of char *strcopy: copies the string s2 into the character array s1. The value of s1 is returned.

Definition of char*strcat: appends the string s2 to the end of character array s1. The first character from s2 overwrites the '' of s1. The value of s1 is returned.

Explanation / Answer

Please find my implementation.

Please let me know in case of any issue.

int mystrlen(const char *pcString)

/* Return the length of string pcString. */

{

const char *pcStringEnd = pcString;

while (*pcStringEnd != '') pcStringEnd++;

return pcStringEnd - pcString;

}

char *strcopy( char *s1, const char *s2) {

char* strresult = s1;

if((NULL != s1) && (NULL != s2))

{

/* Start copy src to dest */

while (NULL != *s2)

{

*s1++ = *s2++;

}

/* put NULL termination */

*s1 = NULL;

}

return strresult;

}

char *strcat(char *s1, const char *s2) {

char* strret = s1;

if((NULL != s1) && (NULL != s2))

{

/* Iterate till end of dest string */

while(NULL != *s1)

{

s1++;

}

/* Copy src string starting from the end NULL of dest */

while(NULL != *s2)

{

*s1++ = *s2++;

}

/* put NULL termination */

*s1 = NULL;

}

return strret;

}

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