본문 바로가기

프로그래밍/C언어

[DGPD][C언어] 문자열 복사 함수 strcpy 만들기.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
 
char* mystrcpy(char* destination, const char* source)
{
    while (*source != '\0')
    {
        *destination = *source;
        source++;
        destination++;
    }
 
    *destination = '\0';
 
    return destination;
}
 
int main()
{
    char str1[] = "sample string";
    char str2[40];
    char str3[40];
    mystrcpy(str2, str1);
    mystrcpy(str3, "copy successful");
    printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
    return 0;
}
cs