#include <stdio.h>
long strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s);
return(s - str);
}
char *strcpy(char *to, const char *from)
{
char *save = to;
for (; (*to = *from) != 0; ++from, ++to);
return(save);
}
char *strcat(char *s, const char *append)
{
char *save = s;
for (; *s; ++s);
while ((*s++ = *append++) != 0);
return(save);
}
int strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)--s2);
}
int main()
{
int result;
char str1[] = "Src string";
char str2[] = "Dst string";
char str3[] = "Dst string";
// strlen test
printf("strlen test\n");
result = strlen(str1);
printf("strlen(str1) = %d\n\n", result);
// strcpy test
printf("strcpy test\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
strcpy(str1, str2);
printf("strcpy(str1, str2)\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n\n", str2);
// strcat test
printf("strcat test\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
strcat(str1, str2);
printf("strcat(str1, str2)\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n\n", str2);
// strcmp test
printf("strcmp test\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
printf("str3 = %s\n\n", str3);
result = strcmp(str1, str2);
if(result == 0)
{
printf("str1 and str2 are same.\n");
}
else
{
printf("str1 and str2 are different.\n");
}
result = strcmp(str2, str3);
if(result == 0)
{
printf("str2 and str3 are same.\n");
}
else
{
printf("str2 and str3 are different.\n");
}
return 0;
}
'컴퓨터 > 언어,프로그래밍' 카테고리의 다른 글
링크드 리스트 정의, 종류 (0) | 2009.05.12 |
---|---|
문자열 연산자 - strcpy, strncpy, strlen, strcat, strncat, strcmp, strncmp, strchar, strstr, atoi, atof, atol, strftime (1) | 2009.05.05 |
malloc 메모리 할당 및 해제 / 재 할당 (0) | 2009.05.05 |
배열 stack / 단순연결리스트 stack (0) | 2009.05.04 |
C언어 :: 링크드 리스트 (0) | 2009.04.24 |