| 태그 'string'에 해당되는 글 1건 |
| strcpy 와 strncpy 구현... |
Tech - https://ohyung.net/591 (YMD: 09/03/29 23:42)
이전 교육 받을때 구현해본 strcpy 와 strncpy...
좀더 최적화를 했더라면 하는 아쉬움이 남지만...

string 헤더 안쓰고 구현 해본다는데 목적이 있었음.
Code Type : C
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
 * Project Name :
 *
 * Version : 1.0
 * Copyright (c) 2009 : Ohyung ( ohyung@ohyung.com )
 * Last modified at : 2009.01.23
 * *****************************************************************************/
 
 
/*******************************************************************************
 *                Include
 ******************************************************************************/
#include "stdio.h"
#include "stdlib.h"
 
 
/*******************************************************************************
 *                Define
 ******************************************************************************/
 
#define PRINTLINE() printf("--------------------------------------------------\n");
 
 
/*******************************************************************************
 *                Code
 ******************************************************************************/
void myprint(char* buff, int max)
{
    int i = 0;
    while(max)
    {
        printf("%c",buff[i++]);
        max--;
    }
    printf("\n");
}
 
#include "string.h"
// mystrcpy
// char* strcpy(char* dest, const char* src);
 
char* mystrcpy(char* dest, const char* src)
{
   int i = 0;
   while (src[i] != '\0')
   {
      dest[i] = src[i];
      i++;
   }
   dest[i] = '\0';
 
    return &dest[0];
}
 
char* mystrncpy(char* dest, const char* src, size_t maxlen)
{
   int i = 0;
   while (src[i] != '\0' && i < maxlen)
   {
      dest[i] = src[i];
      i++;
   }
   while(i < maxlen)
   {
       dest[i] = '\0';
       i++;
   }
 
    return &dest[0];
}
 
int main(void)
{
    char buff_a[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char buff_b[20] = "abcdefghijklmnopq";
 
    char buff_at[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char buff_bt[20] = "abcdefghijklmnopq";
 
    char* result = 0;
 
    printf("origin a : %s \n", buff_a);
    printf("origin b : %s \n", buff_b);
    printf("origin &a or &c : %X , %X \n", &buff_a, &buff_b);
    //printf("origin sizeof(a): %d  sizeof(b): %d \n", sizeof(buff_a), sizeof(buff_b));
 
    PRINTLINE();
    result = strcpy(buff_a, buff_b);
    printf("a : %s \n", buff_a);
    printf("b : %s \n", buff_b);
    printf("&a: %X \n", result);
    memcpy(buff_a, buff_at, strlen(buff_a));
    memcpy(buff_b, buff_bt, strlen(buff_b));
 
    PRINTLINE();
    result = mystrcpy(buff_a, buff_b);
    printf("a : %s \n", buff_a);
    printf("b : %s \n", buff_b);
    printf("&a: %X \n", result);
    memcpy(buff_a, buff_at, strlen(buff_a));
    memcpy(buff_b, buff_bt, strlen(buff_b));
 
    PRINTLINE();
    result = strncpy(buff_a, buff_b, 30);
    printf("a : %s \n", buff_a);
    printf("b : %s \n", buff_b);
    printf("&a: %X \n", result);
    myprint(buff_a, 50);
    memcpy(buff_a, buff_at, strlen(buff_a));
    memcpy(buff_b, buff_bt, strlen(buff_b));
 
 
    PRINTLINE();
    result = mystrncpy(buff_a, buff_b, 30);
    printf("a : %s \n", buff_a);
    printf("b : %s \n", buff_b);
    printf("&a: %X \n", result);
    myprint(buff_a, 50);
 
   return 0;
}
#endif


언제나 그렇듯이 소스가 더럽다는게 문제;
| 이 포스트에 대한 이용규약 |
Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시 2.0 라이센스 에 따라 이용하실 수 있습니다.
This work is licensed under a Creative Commons Attribution 2.0 Korea LicenseLink in a new window.

| 트랙백 |
트랙백 주소 :: https://ohyung.net/rserver.php?mode=tb&sl=591