C实现memomove,memocpy,memoicmp,atoi,itoa函数

memomove实现

   

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <String.h>

void *myMemmove(void *destination, const void *source, unsigned int size) {
    char *pSource = (char *) malloc(size);
    memcpy(pSource, (char *) source, size);
    int i = 0;
    for (; i < size; i++) {
        *(char *) destination++ = pSource[i];
    }
    free(pSource);
    return destination;
}


void main() {
    char str[] = "shaozhongqi from nanjing";
    printf("size = %d", sizeof(str));
    printf("length = %d\n", strlen(str));
    char *p = malloc(sizeof(char) * strlen(str) + 1);
    memcpy(p, str, sizeof(char) * strlen(str) + 1);
    myMemmove(p, str + 1, 4);
    myMemmove(str, str + 1, 4);
    printf("mystr = %s\n", p);
    printf("origin str = %s\n", str);
    free(p);
}

memoicmp:内存指定前面几个字节比较

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int myMemicmp(const void *source1, const void *source2, unsigned int size) {

    if (source1 == NULL && source2 == NULL) {
        return 0;
    }

    if (size == 0) {
        return 0;
    }

    if (source1 == NULL || source2 == NULL) {
        return 0;
    }

    char *pSource1 = (char *) source1;
    char *pSource2 = (char *) source2;
    int i = 0;
    while (*pSource1 == *pSource2 && i < size) {
        pSource1++;
        pSource2++;
        i++;
    }

    if (i == size) {
        return 0;
    }

    if (*pSource1 > *pSource2) {
        return 1;
    } else {
        return -1;
    }

}

void main() {
    char *str1 = "nanjing vvllkkk";
    char *str2 = "nanjing llll";
    int i = myMemicmp(str1, str2, sizeof(char) * 10);
    printf("result = %d\n", i);
}

memcpy实现:

#include <stdio.h>
#include <stdlib.h>
#include <mem.h>


void *myMemcpy(void *destination, const void *soure, size_t length) {
    if (destination == NULL || soure == NULL) {
        return NULL;
    }
    int i = 0;
    char *pDes = (char *) destination;
    char *pSource = (char *) soure;
    for (; i < length; i++) {
        *pDes++ = *pSource++;
    }
    return pDes;
}


void *newMyMemcpy(void *destination, const void *source, int size) {
    if (destination == NULL || source == NULL) {
        return NULL;
    }
    int i = 0;
    char *pDes = (char *) destination;
    char *pSource = (char *) source;

    for (; i < size; pDes++, pSource++, i++) {
        *pDes = *pSource;
    }
    return destination;
}


void mainmemcpy() {
    int a[10] = {1, 22, 33, 44, 5, 66, 77, 88, 99, 100};
    int *p = (int *) malloc(sizeof(int) * 10);
    memcpy(p, a, 40);
    int i = 0;
    for (; i < 10; i++) {
        printf("%4d", p[i]);
    }

    //自带\0
    /*
    char str[11] = "shaozhongqi";
    char* pStr = malloc( sizeof(char) * strlen(str) + 1);
    memcpy(pStr,str,sizeof(char) * strlen(str) );
    pStr[11] = '\0';
    printf("%s\n",pStr);
     */

    char str[100] = "shaohzongqi";
    char *pStr = malloc(sizeof(char) * strlen(str) + 1);
    newMyMemcpy(pStr, str, sizeof(char) * strlen(str) + 1);
    printf("%s\n", pStr);


}
memoset实现:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

void *myMemset(void *source, int value, unsigned int size) {
    if (source == NULL) {
        return NULL;
    }
    int i = 0;
    char *p = (char *) source;
    for (; i < size; i++, p++) {
        *p = value;
    }
    return source;
}

void mainmemset() {
    int a[5] = {1, 2, 3, 4, 5};
    char str[1024] = {"shaozhongqi"};

    //如果是整数就实现清0 0 == '\0'
    //如果是字符串的话就是实现填充
    myMemset(a, 0, 20);
    myMemset(str, 'A', 66);
    myMemset(str, 97, 77);


    int i = 0;
    for (; i < 5; i++) {
        printf("a[%d] = %d\n", i, a[i]);
    }
    printf(str);

}

atoi和itoa实现:atoi支持"+123","-123aa56","123","123dd"格式的字符串

#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#include <stdbool.h>

int myAtoi(const char *source) {
    if (source == NULL) {
        return 0;
    }
    char *pStart = source;
    char *pEnd = source + 1;
    //check invalid number
    bool isPlus = false;

    //判断是否带有符号位
    if (*pStart == '+' || *pStart == '-') {
        isPlus = true;
    }
    //第一个字符不是符号位或者数字就返回0
    if (!(*pStart >= '0' && *pStart <= '9') && *pStart != '+' && *pStart != '-') {
        return 0;
    }

    //找到合法的最后一个数字字符
    while (*pEnd != '\0' && *pEnd >= '0' && *pEnd <= '9') {
        pEnd++;
    }
    int num = 0;
    char *tmp = isPlus ? pStart + 1 : pStart;

    //如果字符串是+a123情况的特殊处理
    if (isPlus && pEnd - pStart <= 1) {
        return 0;
    }

    for (; tmp < pEnd; tmp++) {
        num = num * 10;
        num = num + (*tmp - '0');
    }

    //负数的情况处理
    if (isPlus && *source == '-') {
        num = num * -1;
    }
    return num;

}

char *myItoa(int number, char *source) {
    if (source == NULL) {
        return NULL;
    }
    int length = 0;
    int mNumber = number;
    do {
        length++;
        mNumber = mNumber / 10;
    } while (mNumber != 0);

    int i = length;
    for (; i > 0; i--) {
        int value = number % 10;
        source[i - 1] = value + '0';
        number = number / 10;
    }
    source[length] = '\0';

    return source;

}


void main() {
    char *str = "+a123";
    int number = atoi(str);

    char str2[10] = {0};
    myItoa(number, str2);
    printf("number = %d\n", number);
    printf("str = %s\n", str2);
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值