用c语言.模拟实现strcpy,strcat,strcat,memcpy,me

页面导航:首页 > 软件编程 > C 语言 > 用c语言.模拟实现strcpy,strcat,strcat,memcpy,me

用c语言.模拟实现strcpy,strcat,strcat,memcpy,me

来源: 作者: 时间:2016-01-25 09:50 【

1.模拟实现strcpy#includestdio.h#includestdlib.h#includestring.h#includeassert.hchar *my_strcpy(char *dst, const char *src){ assert(dst != NULL); assert(src != NULL);...
1.模拟实现strcpy
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
char *my_strcpy(char *dst, const char *src)
{
    assert(dst != NULL);
    assert(src != NULL);
    char *ret = dst;
    while ((*dst++ = *src++) != '\0')
        ;
    return ret;
}
int main()
{
    char arr1[] = "hello world!!!";
    char arr2[20];
    my_strcpy(arr2, arr1);
    printf("%s\n", arr2);
    system("pause");
      return 0;
}
2.模拟实现strcat
 
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
char *strcat(char  *dest, char const *src)
{ 
    assert(dest);
    assert(src);
    char *temp = dest;
    while (*dest)
    {
        dest++;
    }
    while (*src)
    {
        *dest++ = *src++;
    }
    *dest = '\0';
    return temp;
 
}
int main()
{
    char arr[50] = "i come from china!!";
        char *p = " me too!!";
        strcat(arr, p);
        printf("%s", arr);
        system("pause");
       return 0;
}
3.模拟实现strcat
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
int my_strcmp(const char *arr1, const char *arr2)
{
    assert(arr1);
    assert(arr2);
    while (*arr1 == *arr2)
    {
        if (*arr1 == '\0')
        {
            return 1;
        }
            arr1++;
            arr2++;
    }
    return *arr1 - *arr2;
}
int main()
{
    char *s1 = "i am a student!";
    char *s2 = "i am a student!";
    int ret=my_strcmp(s1, s2);
    if (ret == 1)
    {
        printf("两个字符串相同!\n");
 
    }
    else{
        printf("两个字符串差值为:%d\n", ret);
 
    }
    system("pause");
    return 0;
}
4.模拟实现memcpy
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
void *my_memcpy(void *dest, const void *src, size_t n)
{
    assert(dest);
    assert(src);
    char *p = (char *)dest;
    const char *q = (char *)src;
    while (n--)
    {
        *p = *q;
        p++;
        q++;
    }
 
    return dest;
 
}
int main()
{
    int arr1[100] = { 1,2,3,4,5,6,7,8,9,10,11 };
    int arr2[100] = { 0 };
    int i = 0;
    int num;
    printf("请输入要拷贝的个数:");
    scanf("%d", &num);
    my_memcpy(arr2, arr1, 4*num);
    for (i = 0; i < num; i++)
    {
        printf("%d ", arr2[i]);
    }
    printf("\n");
    system("pause");
    return 0;
}
5.模拟实现memmove
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
void *my_memcpy(void *dest, const void *src, size_t n)
{
    assert(dest);
    assert(src);
    char *p = (char *)dest;
    const char *q = (char *)src;
    if (p > q && p < q + n)
    {
        while (n--)
        {
          *(p + n) = *(q + n);
        }
    }
    else
    {
        while (n--)
        {
            *p = *q;
            p++;
            q++;
        }
    }
        return dest;
}
int main( )
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int i = 0;
    my_memcpy(arr+4, arr+2, 20);
    for (i = 0; i < 10; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
    system("pause");
    return 0;
}

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<