이전 교육 받을때 구현해본 strcpy 와 strncpy...
좀더 최적화를 했더라면 하는 아쉬움이 남지만...
string 헤더 안쓰고 구현 해본다는데 목적이 있었음.
Code Type : C
/*******************************************************************************
* 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
언제나 그렇듯이 소스가 더럽다는게 문제;