Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Homegrown strncpy and strncat implementation

Max
Max 


Objectives Practice imlpementing an algorithm Practice working with strings in C In this lab, you are going to create your own implementation of the strncpy and strncat functions from the C Library. Recall that the strncpy copies a given number of character elements from a source character array (string) into a destination character array. And information on strncat can be similarly found in the textbook (on page 463). You will write two functions: my_strncpy that replicates the behavior of the built-in strncpy function; and my_strncat that replicates the behavior of the built-in strncat function. Your main method will test these functions out with different source and destination arrays. Remember that a character array can be represented as a character pointer to the first element, as is done in the prototype for strncpy (and strncat) and our own my_strncpy (and strncat) function. NOTE: Please don't use any functions from string.h in this lab, except strlen(). If you do that, you might simply get zero point --- making our grading easier. Requirements: Your my_strncpy function will have the following prototype: char *my_strncpy(char *dest, char *source, int n) Notice that the function returns a pointer to a char. Like the built-in function, you will simply return dest. You will use a loop to assign the first n elements of dest to the first n elements of source. HINT: Even though source and dest are passed in as pointers and not arrays, you can still use the subscript [ i ] notation to access the ith element. You could also use pointer arithmetic to increment source and dest to point to their next element on each iteration. Remember that the null character \0 must be appended to dest at index n Your function does not need to do any safety checks to make sure n is less than the length of dest. Your my_strncat will also have the following prototype: char *my_strncat(char *dest, char *source, int n), more can be found in the textbook (on page 463) Test your functions by inserting your my_strncat and my_strncpy functions into the appropriate place in the following program. /* * Homegrown strncpy test program */ #include #include // Function prototypes char *my_strncpy(char *dest, char *source, int n); char *my_strncat(char *dest, char *source, int n); // ********* Insert your my_strncpy implementation here! ************* // ********* Insert your my_strncat implementation here! ************* int main() { // Test your function with various source strings and values of n char testSrc[] = "Hello"; char testDest[40] = "world"; int n = 5; my_strncat(testDest, testSrc, n); printf("CAT: %s \n", testDest); my_strncpy(testDest, testSrc, n); printf("CPY: %s \n", testDest); printf("%s \n", testDest); return 0; } Attachments
Tags Clarifications

Answers

Program code for my_strcat(), my_strcpy()

vkbhardwaj
vkbhardwaj 



Here this file contains user defined programs of strcat(),and strcpy(). Attachments Screenshots
Purchase Answer