Your assignment is to write your own version of some of the functions in the bui
ID: 3819659 • Letter: Y
Question
Your assignment is to write your own version of some of the functions in the
built-in <string.h> C library.
4. Write a function called strncpy406. It is passed 3 parameters: 2 strings
and the length of the destination array (the first parameter). The intent of
including a third parameter is to prevent overflow of the destination array
in case the source array (the second parameter) contains a string that is too
long to be stored in the destination. strncpy406 returns a pointer to the
destination array.
For example:
char s1[] = "comp";
char s2[] = "systems";
strncpy(s1, s2, 4);
printf("%s ");
The output of the above code should be syst.
printf(" Problem 4 ");
char ababc[] = "abcbc";
char *s = ababc;
char *sb = strchr406(s, 'b');
printf("Address of 'b' in %s is %x ", ababc, sb);
printf("String is %s ", sb);
char *sc = strchr406(s,'c');
printf("Address of 'c' in %s is %x ", ababc, sc);
printf("String is %s ", sc);
char *sd = strchr406(s,'d');
printf("Address of 'd' in %s is = %d ", ababc, sd);
Explanation / Answer
char* strncpy406(char *destination, const char *source, int lenDestStr)
{
if(strlen(source)<=lenDestStr)
{
while(*source!='o')
{
*destination=*source;
destination++;
source++;
}
*destination='o';
return destination;
}
}
char* strchr406(char *str,char ch)
{
while(*str!='o')
{
if(*str==ch)
return str;
}
return NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.