Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homegrown strncpy implementation C programming Objectives Practice imlpementing an algorithm Practice working with strings in C In this lab, you are going to create your

Homegrown strncpy implementation C programming

Objectives

Practice imlpementing an algorithm

Practice working with strings in C

In this lab, you are going to create your own implementation of thestrncpyfunction 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. You will write a functionmy_strncpythat replicates the behavior of the built-in strncpy function. Your main method will test this function 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 our own my_strncpy function. NOTE: don't use any functions from string.h in this lab. 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 returndest.

You will use a loop to assign the firstnelements ofdestto the first n elements ofsource.

HINT:Even thoughsourceanddestare 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 todestat indexn

Your function does not need to do any safety checks to make sure n is less than the length of dest.

Test your function by inserting your my_strncpy function into the appropriate place in the following program.

/*

* Homegrown strncpy test program

*/

#include

// Function prototypes

char *my_strncpy(char *dest, char *source, int n);

// ********* Insert your my_strncpy implementation here! *************

int main() {

// Test your function with various source strings and values of n

char testSrc[] = "Hello";

char testDest[40];

int n = 5;

my_strncpy(testDest, testSrc, n);

printf("%s ", testDest);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Combinatorial Testing In Cloud Computing

Authors: Wei-Tek Tsai ,Guanqiu Qi

1st Edition

9811044805, 978-9811044809

More Books

Students also viewed these Programming questions