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:

Anonymous said...

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

April 9, 2008 at 2:27 AM
Tarkesh Mahajan said...

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

April 24, 2008 at 12:51 AM
Anonymous said...

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

July 16, 2008 at 8:20 AM
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

April 21, 2010 at 10:13 PM
Chethan S said...

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

October 14, 2011 at 12:04 AM
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.

December 24, 2011 at 11:56 PM
Shek said...

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

May 6, 2012 at 9:51 AM
Anonymous said...

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

July 2, 2012 at 4:08 PM
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

September 21, 2012 at 12:18 AM
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

April 16, 2013 at 2:53 PM
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

May 4, 2013 at 3:44 AM
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!

January 31, 2016 at 11:53 PM
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


October 14, 2016 at 2:58 AM
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


October 14, 2016 at 2:58 AM
polison said...

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

May 12, 2022 at 2:15 PM
nacho said...

x1z42c3o50 l8u78k5w96 e6n81j8d47 o9h28y0q64 g6y27v9r14 o4c35f5u10

August 24, 2022 at 9:47 PM
theslol said...

o8j93e4l21 p8t58y1q55 r7c57h0y91 k7k84j6w27 t8e45n9k94 k7v07m2y56

September 30, 2022 at 4:46 PM
Anonymous said...

mwehwe78
golden goose outlet
golden goose outlet
golden goose outlet
golden goose outlet
golden goose outlet
golden goose outlet
golden goose outlet
supreme outlet
golden goose outlet
golden goose outlet

May 31, 2025 at 4:31 AM

Post a Comment

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /