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?
If you consider of C Library functions, it takes care of buffer overlapped, Src and Dest can be overlapped.
ReplyDeleteThe purpose of this question is to implement w/o using the std in built libraries
ReplyDeletechar * strcpy( char *strDest, const char *strSrc )
ReplyDelete{
assert( (strDest != NULL) && (strSrc != NULL) );
char *address = strDest;
while( (*strDest++ = * strSrc++) != ‘\0’ );
return address;
}
Hi,
ReplyDeleteYou 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
Mystrcpy(target,source)
ReplyDelete{
*q=*p;
q++;
P++;
}
Sample C Program To Accept A String & Display It.
ReplyDeleteSample 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.
None of the code says how to allocate memory to the destination.
ReplyDeletevoid stringCopy(Char* source, char* destination)
ReplyDelete{
while(*source != 0)
{
*destination = *source;
++source;
++destinaton;
}
*destination = 0;
}
Here's one from K&R[1]
ReplyDeletevoid strcpy(char *s, char*t)
{
while ( *s++ = *t++)
;
}
[1] The C Programming Language, Kernighan & Ritchie
Hello I am so grateful I found your blog page, I really found you by accident, while I was researching on Digg
ReplyDeletefor 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
I love it when people get together and share views.
ReplyDeleteGreat site, continue the good work!
Also visit my web site - how much should i weigh calculator
Hi all,
ReplyDeleteI'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!
ReplyDeleteVery 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
ReplyDeleteVery 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
navigate to this website Louis Vuitton fake Bags YOURURL.com Ysl replica description replica gucci bags
ReplyDeletex1z42c3o50 l8u78k5w96 e6n81j8d47 o9h28y0q64 g6y27v9r14 o4c35f5u10
ReplyDeleteo8j93e4l21 p8t58y1q55 r7c57h0y91 k7k84j6w27 t8e45n9k94 k7v07m2y56
ReplyDeletemwehwe78
ReplyDeletegolden 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