Saturday, July 7, 2007

Implement the strcpy() function.

Here are some C programs which implement the strcpy() function. This is one of the most frequently asked C interview questions.


Method1


char *mystrcpy(char *dst, const char *src)
{
char *ptr;
ptr = dst;
while(*dst++=*src++);
return(ptr);
}


The strcpy function copies src, including the terminating null character, to the location specified by dst. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap. It returns the destination string. No return value is reserved to indicate an error.

Note that the prototype of strcpy as per the C standards is


char *strcpy(char *dst, const char *src);


Notice the const for the source, which signifies that the function must not change the source string in anyway!.

Method2


char *my_strcpy(char dest[], const char source[])
{
int i = 0;
while (source[i] != '\0')
{
dest[i] = source[i];
i++;
}
dest[i] = '\0';
return(dest);
}


Simple, isn't it?

18 comments:

  1. If you consider of C Library functions, it takes care of buffer overlapped, Src and Dest can be overlapped.

    ReplyDelete
  2. The purpose of this question is to implement w/o using the std in built libraries

    ReplyDelete
  3. char * strcpy( char *strDest, const char *strSrc )
    {
    assert( (strDest != NULL) && (strSrc != NULL) );
    char *address = strDest;
    while( (*strDest++ = * strSrc++) != ‘\0’ );
    return address;
    }

    ReplyDelete
  4. Hi,

    You have good collection of Interview Question and Answer. you can also share your question and answer in this website

    http://www.iquestionanswer.com
    http://www.iquestionanswer.net

    ReplyDelete
  5. Mystrcpy(target,source)
    {
    *q=*p;
    q++;
    P++;
    }

    ReplyDelete
  6. None of the code says how to allocate memory to the destination.

    ReplyDelete
  7. void stringCopy(Char* source, char* destination)
    {
    while(*source != 0)
    {
    *destination = *source;
    ++source;
    ++destinaton;
    }
    *destination = 0;
    }

    ReplyDelete
  8. Here's one from K&R[1]

    void strcpy(char *s, char*t)
    {
    while ( *s++ = *t++)
    ;
    }

    [1] The C Programming Language, Kernighan & Ritchie

    ReplyDelete
  9. Hello I am so grateful I found your blog page, I really found you by accident, while I was researching on Digg
    for something else, Anyhow I am here now and would just like
    to say cheers for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have bookmarked it and also added in your RSS feeds, so
    when I have time I will be back to read a great deal more,
    Please do keep up the great work.

    my web site; jocuri online masini

    ReplyDelete
  10. I love it when people get together and share views.
    Great site, continue the good work!

    Also visit my web site - how much should i weigh calculator

    ReplyDelete
  11. Hi all,
    I'm a pretty young coder.
    May a ask you regarding your first method:

    char *mystrcpy(char *dst, const char *src)
    {
    char *ptr;
    ptr = dst;
    while(*dst++=*src++);
    return(ptr);
    }

    When does the while loop terminate? I thought this would be endless since no checking ='\0' is performed?

    Thx in advance!

    ReplyDelete


  12. Very informative article.Thank you author for posting this kind of article .



    http://www.wikitechy.com/view-article/string-copy-program-in-c-example-with-explanation



    Both are really good,
    Cheers,
    Venkat


    ReplyDelete


  13. Very informative article.Thank you author for posting this kind of article .



    http://www.wikitechy.com/view-article/string-copy-program-in-c-example-with-explanation



    Both are really good,
    Cheers,
    Venkat


    ReplyDelete