Tuesday 14 May 2019

c - How to write recursive function that copies a string to a location?

any ideas about how to write a RECURSIVE function that gets 2 parameters :
first is an address d (a location of char).
second is a string.
The function copies the string s to a location starting at d.
The function returns d as a result !
can we do it without strcpy ?




    copy_r(char *s, char *d)
{
*d = *s;
if(*s)return copy_r(++s, ++d);
}


where is the mistake ? (found )
put still there is a problem ! what if the location d overlaps some location that is already occupied by s ?
this for example
strcpy(p1, "abcdefghijklomopqrstuvwqyz"); printf(copy_r(p1, p1+10));doesnt work –




output should be klomopqrstuvwqyz

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...