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?

17 comments:

Anonymous said...

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

Tarkesh Mahajan said...

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

Anonymous said...

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

Anonymous said...

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

Chethan S said...

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

Uttam Agrawal said...

Sample C Program To Accept A String & Display It.

Sample C Program To Find The Length Of A String.

Sample C Program To Concatenate Two Strings.

Sample C Program To Compare Two Strings.

Sample C Program To Swap Two Strings.

Sample C Program To Swap Two Strings Using strcpy() Function.

Sample C Program To Sort A Given Number Of Strings Using strcmp() Function.

Sample C Program To Check Whether A String Is Palindrome Or Not.

Sample C Program To Print The Reverse Of A String.

Sample C Program To Join Two Strings.

Sample C Program To Display Array Of Strings.

Sample C Program To Convert String To An Integer Using atoi() Function.

Sample C Program To Accept A String & Display In Reverse.

Sample C Program To Accept A String & Display Its Alternate Characters.

Sample C Program To Accept A String & Display Alternate Characters In Either Case.

Shek said...

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

Anonymous said...

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

Unknown said...

Here's one from K&R[1]

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

[1] The C Programming Language, Kernighan & Ritchie

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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!

Unknown said...



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


Unknown said...



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


polison said...

navigate to this website Louis Vuitton fake Bags YOURURL.com Ysl replica description replica gucci bags

nacho said...

x1z42c3o50 l8u78k5w96 e6n81j8d47 o9h28y0q64 g6y27v9r14 o4c35f5u10

theslol said...

o8j93e4l21 p8t58y1q55 r7c57h0y91 k7k84j6w27 t8e45n9k94 k7v07m2y56