C Program low level strcpy

How to write a C Program low level strcpy in C Programming Language ?


This C Program low level strcpy. Wipe the string first
     -This is so that you don't end up with "Hello" mutating to something like "Hello5" if dest is e.g. "098765".

Solution:

  1. void copy_string(char *src, char *dest){
  2.     char *ps = src,
  3.          *pd = dest;
  4.  
  5.     /* Wipe the string first
  6.      * This is so that you don't end up with "Hello" mutating to
  7.      * something like "Hello5" if dest is e.g. "098765"
  8.      */
  9.     while(*pd){
  10.         *pd = 0;
  11.         pd++;
  12.     }
  13.     /* Reset pd */
  14.     pd = dest;
  15.  
  16.     /* Finally, copy our source */
  17.     while(*ps){
  18.         *pd = *ps;
  19.         pd++;
  20.         ps++;
  21.     }
  22. }


Learn More :