This is in the C Write a function called my_str_n_cat() that accepts pointer to
ID: 3696249 • Letter: T
Question
This is in the C
Write a function called my_str_n_cat() that accepts pointer to a destination character array, a pointer to a source character array (which is assumed to be a string), and a integer n, and returns the pointer to the destination character array. This function needs to copy at most n characters, character by character, from the source character array to the end of the destination character array. If a null character is encountered before n characters have been encountered, copying must stop. You may NOT use any functions found in to solve this problem! Note: you may use array or pointer notation in this function.
Explanation / Answer
Following is thefunction called my_str_n_cat() that accepts pointer to a destination character array, a pointer to a source character array (which is assumed to be a string), and a integer n, and returns the pointer to the destination character array. This function needs to copy at most n characters, character by character, from the source character array to the end of the destination character array
--------------------------------------------------------------------------------------------------
char * my_str_n_cpy( char *d, const char *s, size_t n ) {
for ( char *p= d; n > 0 && *s != ''; --n )
*p++= *s++;
return d;
}
What you quoted didn't mention the third parameter. But I know it needs to be there. If you don't like "size_t" as the type, use (int) or (unsigned int) or whatever else you prefer there. size_t is found in stdlib.h.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.