C语言模拟实现str/mem字符串函数

2019-07-22 0 条评论 72 次阅读 2 人点赞

1.模拟实现strlen

#include <stdio.h>
#include <string.h>

size_t Strlen(const char* str){
    size_t len = 0;
    for(; str[len] != 0; ++len){
        //循环体中不需要其他代码了
    }
    return len;
}

int main(){
    char* str = "hehe";
    int len = Strlen(str);
    printf("%d\n" , len);
    return 0;
}

2.模拟实现strcpy

#include <stdio.h>
#include <string.h>
#include <assert.h>

char* Strcpy(char* dest , const char* src){//des目标 src原字符串
    //函数实现者的角度来保证参数有效
    //只能一定程度的保证
    //参数校验
    assert(src != NULL && dest != NULL);
    int i = 0;
    for (; src[i] != '\0' ; ++i) {
        dest[i] = src[i];
    }
    dest[i] = '\0';
    return dest;
}

int main(){
    char str1[100] = "hehe";
    char str2[100] = "hahaha";
    Strcpy(str1 , str2);
    printf("%s\n",str1);
    return 0;
}

3.模拟实现strcat

#include <stdio.h>
#include <string.h>

char* Strcat(char* dest, const char* src){
    if(dest == NULL || src == NULL){
        return dest;
    }
    //1.先找到dest的末尾
    char* p = dest;
    while (*p != '\0'){
        ++p;
    }
    //2.拷贝
    while(*src != '\0'){
        *p = *src;
        ++p;
        ++src;
    }
    //3.拷贝'\0'
    *p = '\0';
    return dest;
}
int main(){
    char str[1024] = "We are ";
    Strcat(str,"family.");
    printf("%s",str);
    return 0;
}

4.模拟实现strcmp

#include <assert.h>
#include <stdio.h>
#include <string.h>

int Strcmp(const char* str1, const char* str2){
    assert(str1 != NULL && str2 != NULL);
    while (str1 != '\0' && *str2 != '\0'){
        if(*str1 < *str2){
            return -1;
        } else if (*str1 > *str2){
            return 1;
        } else {
            ++str1;
            ++str2;
        }
    }
    if (*str1 < *str2){
            return -1;
        } else if (*str1 > *str2){
            return 1;
        } else {
            return 0;
        }
}

int main(){
    char* s1 = "LiuQingMing";
    char* s2 = "LiiuKingMing";
    int ret = Strcmp(s1, s2);
    return 0;
}

5.模拟实现strstr

char* Strstr(const char* str1, const char* str2){
    if(str1 == NULL || str2 == NULL){
        return NULL;
    }
    if(*str2 == '\0'){

    }
    char *black_ptr = str1;
    while(*black_ptr != '\0'){
        char* red_ptr = black_ptr;
        char* sub_ptr = str2;
        while (*red_ptr != '\0' && *sub_ptr != '\0'
        && (*red_ptr == *sub_ptr)){
            ++red_ptr;
            ++sub_ptr;
        }
        if(*sub_ptr == '\0'){
            return black_ptr;
        }
    }
    return NULL;
}

int main(){
    char str[] = "This is a simple string";
    char* pch = Strstr(str,"simple");
    printf("%s",pch);
    return 0;
}

6.模拟实现strncpy

#include <stdio.h>
#include <assert.h>
char* Strncpy(char* destination,const char* source,size_t num){
    assert(destination&&source&&num);
    for (int i = 0; i <num; ++i) {
        *destination++ = *source++;
    }
    *destination = *source;
    return destination;
}
int main(){
    char str1[] = "To be or not to be";
    char str2[40];
    char str3[40];
    Strncpy(str2,str1, sizeof(str2));
    Strncpy(str3,str2,5);
    str3[5] = '\0';
    puts(str1);
    puts(str2);
    puts(str3);
    return 0;
}

7.模拟实现strncat

#include <stdio.h>
#include <assert.h>
#include <string.h>
char* Strncat(char* destination, const char* source,size_t num){
    assert(destination&&source&&num);
    char* flag = destination;
    while (*flag!='\0'){
       ++flag;
    }
    for (int i = 0; i < num; ++i) {
        *flag = *source;
        ++flag;
        ++source;
    }
}
int main(){
    char str1[20];
    char str2[20];
    strcpy(str1,"To be ");
    strcpy(str2,"or not to be.");
    strncat(str1,str2,6);
    puts(str1);
    return 0;
}

8.模拟实现strncmp

#include <stdio.h>
#include <assert.h>
int Strncmp(const char* str1, const char* str2,size_t num){
    assert(str1&&str2);
    while (num&&*str1 == *str2){
        if(*str1=='\0'||*str2=='\0'){
            break;
        }
        ++str1;
        ++str2;
        --num;
    }
    if(num==0){
        return 0;
    }
    return *str1 - *str2;
}
int main(){
    char str[][5] = {"R2D2","C3P0","R2A6"};
    int n;
    puts("Looking for R2 astromech droids...");
    for(n=0;n<3;n++){
        if(Strncmp(str[n],"R2xx",2)==0){
            printf("found %s\n",str[n]);
        }
    }
    return 0;
}

9.模拟实现memcpy

void* Memcpy(void* dest, const void* src, size_t num){
    if (dest == NULL || src == NULL ){
        return dest;
    }
    char* pdest = (char*)dest;
    char* psrc = (char*)src;
    for(size_t i = 0; i < num; ++i){
        *pdest = *psrc;
        ++pdest;
        ++psrc;
    }
    return dest;
}

int main(){
    int arr[4] = {1, 2, 3, 4};
    int arr2[4] = {0};
    Memcpy(arr2, arr, sizeof(arr));
    for(int i = 0; i < 4; ++i){
        printf("%d\n", arr2[i]);
    }
    return 0;
}

10.模拟实现memmove

void* Memmove(void* dest, const void* src, size_t num){
    if (dest == NULL || src == NULL ){
        return dest;
    }
    char* pdest = (char*)dest;
    char* psrc = (char*)src;
    if(pdest >= psrc && pdest < psrc + num){
        //缓冲区重叠, 从后往前拷贝
        pdest = pdest + num - 1;
        psrc = psrc + num - 1;
        for (size_t i = 0; i < num; ++i){
            *pdest = *psrc;
            --pdest;
            --psrc;
        }
    } else {
        //不重叠, 正常拷贝
        for(size_t i = 0; i < num ; ++i){
            *pdest = *psrc;
            ++pdest;
            ++psrc;
        }
    }
    return dest;
}
int main(){
    char str[] = "memmove can be very useful...";
    Memmove(str+20,str+15,11);
    puts(str);
    return 0;
}

11.模拟实现memcmp

void* Memcmp(void* const ptr1, const void* ptr2, size_t num){
    assert(ptr1 != 0 && ptr2 != 0 && num > 0);

    char* p1 = (char*)ptr1;
    char* p2 = (char*)ptr2;
    for(size_t i = 0; i < num; i++){
        if(*p1 < * p2){
            return -1;
        } else if (*p1 > *p2){
            return 1;
        } else {
            ++p1;
            ++p2;
        }
    }
    return 0;
}

L_KingMing

这个人太懒什么东西都没留下

文章评论(0)