strcpy c implementation

Solutions on MaxInterview for strcpy c implementation by the best coders in the world

showing results for - "strcpy c implementation"
Paulina
18 Apr 2019
1#include <stdio.h>
2 
3// Function to implement strcpy() function
4char* strcpy(char* destination, const char* source)
5{
6    // return if no memory is allocated to the destination
7    if (destination == NULL)
8        return NULL;
9 
10    // take a pointer pointing to the beginning of destination string
11    char *ptr = destination;
12 
13    // copy the C-string pointed by source into the array
14    // pointed by destination
15    while (*source != '\0')
16    {
17        *destination = *source;
18        destination++;
19        source++;
20    }
21 
22    // include the terminating null character
23    *destination = '\0';
24 
25    // destination is returned by standard strcpy()
26    return ptr;
27}
28 
29// Implement strcpy function in C
30int main(void)
31{
32    char source[] = "Techie Delight";
33    char destination[25];
34 
35    printf("%s\n", strcpy(destination, source));
36 
37    return 0;
38}
39
Mats
12 Aug 2017
1while ((*destination++ = *source++) != '\0');
2