Skip to content

Instantly share code, notes, and snippets.

@akayj
Last active August 29, 2015 13:56
Show Gist options
  • Save akayj/9249023 to your computer and use it in GitHub Desktop.
Save akayj/9249023 to your computer and use it in GitHub Desktop.
strcpy from scratch
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *my_strcpy(char *strDest, const char *strSrc)
{
if (strSrc == NULL || strDest == NULL)
return NULL;
char *dest = strDest;
while ((*strDest++ = *strSrc++) != '\0');
return dest;
}
int main()
{
char *src = "Hello world";
char *dest = (char *)malloc(strlen(src)+1);
my_strcpy(dest, src);
printf("%s\n", dest);
free(dest);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment