#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;
}