strcpy implementation in c

#include <stdio.h>
#include <string.h>

int main() {
   char str1[20] = "C programming";
   char str2[20];

   // copying str1 to str2
   strcpy(str2, str1);

   puts(str2); // C programming

   return 0;
}

4.33
3
Phoenix Logan 186120 points

                                    #include &lt;stdio.h&gt;
&nbsp;
// Function to implement strcpy() function
char* strcpy(char* destination, const char* source)
{
&nbsp;&nbsp;&nbsp;&nbsp;// return if no memory is allocated to the destination
&nbsp;&nbsp;&nbsp;&nbsp;if (destination == NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// take a pointer pointing to the beginning of destination string
&nbsp;&nbsp;&nbsp;&nbsp;char *ptr = destination;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// copy the C-string pointed by source into the array
&nbsp;&nbsp;&nbsp;&nbsp;// pointed by destination
&nbsp;&nbsp;&nbsp;&nbsp;while (*source != '\0')
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*destination = *source;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destination++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source++;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// include the terminating null character
&nbsp;&nbsp;&nbsp;&nbsp;*destination = '\0';
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// destination is returned by standard strcpy()
&nbsp;&nbsp;&nbsp;&nbsp;return ptr;
}
&nbsp;
// Implement strcpy function in C
int main(void)
{
&nbsp;&nbsp;&nbsp;&nbsp;char source[] = &quot;Techie Delight&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;char destination[25];
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s\n&quot;, strcpy(destination, source));
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}

4.33 (3 Votes)
0
4
4
Awgiedawgie 440220 points

                                    while ((*destination++ = *source++) != '\0');

4 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source