#include <stdio.h>
#include <string.h>
int my_strlen( const char *str)
{
int count = 0;
while(*str)
{
count++;
str++;
}
return count;
}
char *my_strcpy(char *dst, const char *src)
{
if( src == NULL)
{
return NULL;
}
char *tmp = dst;
while(*src)
{
*dst = *src;
dst++;
src++;
}
*dst = '\0';
dst = tmp;
return dst;
}
char *my_strncpy(char *dst, const char *src, int nbytes)
{
if( src == NULL)
{
return NULL;
}
char *tmp = dst;
while(*src && nbytes)
{
*dst = *src;
dst++;
src++;
nbytes--;
}
*dst = '\0';
dst = tmp;
return dst;
}
char *my_strcat(char *dst, const char *src)
{
if( src == NULL || dst == NULL)
{
return NULL;
}
char *tmp = dst;
while(*dst)
{
dst++;
}
while(*src)
{
*dst = *src;
dst++;
src++;
}
*dst = '\0';
dst = tmp;
return dst;
}
int my_strcmp(const char *s1, const char *s2)
{
while(*s1 == *s2)
{
if(*s1 == '\0')
{
return 0;
}
if(s1 < s2)
{
return -1;
}
s1++;
s2++;
}
return 1;
}
int main()
{
char str[1000];
char src_str[1000];
char dst_str[1000];
int cmd = 0;
int str_len = 0;
int nbytes = 0;
int ret;
while(1)
{
printf("=================show the func=================\n");
printf("1.calc the str len\n");
printf("2.copy the src_str to dst_str\n");
printf("3.copy the src_str n bytes to dst_str\n");
printf("4.link the src_str to dst_str\n");
printf("5.compare the str1 and str2\n");
printf("===============================================\n");
printf("please input your cmd:\n");
scanf("%d",&cmd);
if (0 == cmd)
{
break;
}
switch(cmd)
{
case 1:
{
printf("input your scr_str:\n");
scanf("%s",str);
str_len = my_strlen(str);
printf("str_len = %d\n",str_len);
// printf("%d\n",strlen(str));
}break;
case 2:
{
printf("input your scr_str:\n");
scanf("%s",src_str);
my_strcpy( dst_str,src_str);
printf("%s\n",dst_str);
}break;
case 3:
{
printf("input your scr_str:\n");
scanf("%s",src_str);
printf("input the bytes you want to copy:\n");
scanf("%d",&nbytes);
my_strncpy( dst_str,src_str,nbytes);
printf("%s\n",dst_str);
}break;
case 4:
{
printf("input your scr_str:\n");
scanf("%s",src_str);
printf("input your dst_str:\n");
scanf("%s",dst_str);
my_strcat( dst_str,src_str);
printf("dst_str = %s\n",dst_str);
}break;
case 5:
{
printf("input your scr_str:\n");
scanf("%s",src_str);
printf("input your dst_str:\n");
scanf("%s",dst_str);
ret = my_strcmp( dst_str,src_str);
printf("ret = %d\n",ret);
}break;
}
}
return 0;
}