strlen,strcpy,strcat,strcmp,strstr,memcpy,的模拟实现方式

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>
#include<assert.h>
//模拟实现strlen三种实现方式
int my_strlen_1(const char* str)
{
    //使用计数器实现
    int count = 0;
    assert(str);
    while(*str)
    {
        count++;
        str++;
    }
    return count;
}
//第二种递归实现
int my_strlen_2(const char* str)
{
    assert(str);
    if(*str != '\0')
        return 1+my_strlen_2(str+1);
    else
        return 0;
}
//指针减指针模拟实现
int my_strlen_3(const char* str)
{
    const char *p = str;  
    while(*p)  
    {  
        p++;  
    }  
    return p-str;
}
int main()
{
    int len1 = my_strlen_1("abcdef");
    int len2 = my_strlen_2("abcdef");
    int len3 = my_strlen_3("abcdef");
    printf("len1 = %d\n",len1);
    printf("len2 = %d\n",len2);
    printf("len3 = %d\n",len3);
    system("pause");
    return 0;
}

//模拟实现strcpy
char* my_strcpy(char* dest,const char* src)
{
    char *ret = dest;
    assert(src != NULL);
    assert(dest != NULL);
    while(*dest++ = *src++)
    {
        ;
    }
    return ret;
}

int main()
{
    char arr[80]={0};
    printf("string=%s\n",my_strcpy(arr,"hello world"));
    system("pause");
    return 0;
}
//模拟实现strcat
char *my_strcat(char *dest,char *src)
{
    char *ret = dest;
    assert(dest);
    assert(src);
    while(*dest)
    {
        dest++;
    }
    while(*dest++ = *src++)
    {
        ;
    }
    return ret;
}

int main()
{
    char arr1[15]="abcd";
    char *arr2="ef";
    printf("%s\n",my_strcat(arr1,arr2));
    system("pause");
    return 0;
}
//模拟实现strcmp
int my_strcmp(const char* arr1,const char* arr2)
{
  assert(arr1);
  assert(arr2);
  while(*arr1 == *arr2)
  {
   if(*arr1 == '\0')
     return 0;
   arr1++;
   arr2++;
  }
 return *arr1 - *arr2;
}

int main()
{
 char *arr1="abcdef";
 char *arr2="abcd";
 int ret=my_strcmp(arr1,arr2);
 printf("%d\n",ret);
 system("pause");
 return 0;
}

//模拟实现strstr
char* my_strstr(const char* str, const char* substr)  
{  
    const char* s1 = str; 
    const char* s2 = substr;  
    const char* ret = str;//ret就是用来保存首地址的  
    assert(str);  
    assert(substr);
    if(strlen(s1)<strlen(s2))
        return NULL;
    while (*ret)  
    {  
        s1 = ret;s2 = substr;  
        while (*s1 && *s2 && (*s1 == *s2))  
        {  
            s1++;  
            s2++;  
        }  
        if (*s1 == '\0')  
        {  
            return NULL;  
        }  
        if(*s2 == '\0')
            return ret; 
        ret++;
    }  
    return NULL;  
}  
int main()  
{  
    char a[20] = "abbbcdef";  
    char b[10] = "bbcde";  
    printf("%s\n", my_strstr(a, b));  
    system("pause");  
    return 0;  
}  
//模拟实现memcpy

void* my_memcpy(void* dest, void* src,size_t n)  
{  
    void* ret = dest;  
    char* str1 = (char*)dest;  
    char* str2 = (char*)src;  
    assert(dest);  
    assert(src);  
    while (n--)  
    {  
        *str1++ = *str2++;  
    }  
    return ret;  
}  

void* my_memmove(void* dest, void* src, size_t n)  
{  
    void* ret = dest;  
    char* str1 = (char*)dest;  
    char* str2 = (char*)src;  
    assert(dest);  
    assert(src);  
    if (str1 > str2)   
    {  
        while (n--)  
        {  
            *(str1 + n) = *(str2 + n);  
        }  
    }  
    else  
    {  
        while (n--)  
        {  
            *str1++ = *str2++;  
        }  
    }  
    return ret;  
}  

int main()  
{  
    int i = 0;  
    int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
    int arr2[10] = { 0 };  
    int arr3[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
    my_memcpy(arr2, arr1, sizeof(arr1));  
    my_memmove(arr3 + 5, arr3 + 4, 3 * sizeof(int));  
    for (i = 0; i < 10; i++)  
    {  
        printf("%d ", arr2[i]);  
    }  
    printf("\n");  
    for (i = 0; i < 10; i++)  
    {  
        printf("%d ", arr3[i]);  
    }  
    system("pause");  
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值