程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用c語言.模擬實現strcpy,strcat,strcat,memcpy,memmove

用c語言.模擬實現strcpy,strcat,strcat,memcpy,memmove

編輯:關於C語言

用c語言.模擬實現strcpy,strcat,strcat,memcpy,memmove


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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved