• 【算法】【字符串】C语言常用库函数实现


    strcpy

    #include <iostream>
    #include <assert.h>
    using namespace std;
    
    char * my_strcpy(char* str1,const char* str2)
    {
        char* p = str1;
        assert(str1 != NULL && str2 != NULL);
        while((*p++ = *str2++) != '');
        return p;
    }
    
    
    int main()
    {
        const char str[] = "abcdef";
        char s[] = "";
        my_strcpy(s, str);
        cout << s << endl;
        return 0;
    }
    

    strcat

    #include <iostream>
    #include <vector>
    #include <assert.h>
    using namespace std;
    
    char* my_strcat(char* dest, const char* src)
    {
        assert(dest != NULL && src != NULL);
        char *p = dest;
        while(*p != '') p++;
        while((*p++ = *src++) != '');
        return p;
    }
    
    int main()
    {
        const char str[] = "abcdef";
        char s[] = "e";
        my_strcat(s, str);
        cout << s << endl;
        return 0;
    }
    

    reverse

    #include <iostream>
    #include <vector>
    #include <assert.h>
    using namespace std;
    
    char* my_reverse(char* str)
    {
        assert(str != NULL);
        char *p = str;
        int len = 0;
        while(*p++ != '') len++;
        int l = 0, r = len - 1;
        while(l < r)
        {
            str[l] = str[l] ^ str[r];
            str[r] = str[l] ^ str[r];
            str[l] = str[l] ^ str[r];
    
            l++;
            r--;
        }
        return str;
    }
    
    int main()
    {
        char str[] = "abcdef";
        cout << my_reverse(str) << endl;
        cout << str << endl;
        return 0;
    }
    

    strstr

    #include <iostream>
    #include <vector>
    #include <assert.h>
    using namespace std;
    
    char* my_strstr(char* str1, char* str2)
    {
        assert(str1 != NULL && str2 != NULL);
        while (*str1 != '')
        {
            char* src = str1;
            char* tmp = str2;
            do
            {
                if (*tmp == '')
                    return str1;
            } while (*tmp++ == *src++);
            str1++;
        }
        return NULL;
    }
    
    int main()
    {
        char str[] = "abcdef";
        char str2[] = "x";
        if (my_strstr(str, str2) != NULL)
            cout << my_strstr(str, str2) << endl;
        else
            cout << "NULL" << endl;
        return 0;
    }
    

    memcpy

    #include <iostream>
    #include <vector>
    #include <assert.h>
    using namespace std;
    
    void* my_memcpy(void* dest, void* src, size_t size)
    {
        assert(dest != NULL && src != NULL && size > 0);
        char *tmp_dst = (char *)dest;
        char *tmp_src = (char *)src;
        if(tmp_src < tmp_dst && tmp_dst < tmp_src + size)
        {
            tmp_dst = tmp_dst + size - 1;
            tmp_src = tmp_src + size - 1;
            while(size--) *tmp_dst-- = *tmp_src--;
        }
        else
            while(size--) *tmp_dst++ = *tmp_src++;
    
        return dest;
    }
    
    
    int main()
    {
        char str[] = "abcdef";
        cout << my_memcpy(str + 2, str, 2) << endl;
        cout << str << endl;
        return 0;
    }
    

    atoi

    #include <iostream>
    #include <vector>
    #include <assert.h>
    using namespace std;
    
    int my_atoi(char *str)
    {
        assert(str != NULL);
        char *tmp = str;
        while(*tmp != '' && *tmp == ' ') tmp++;
    
        int flag = 0;
        if(*tmp == '-') flag = 1, tmp++;
        if(*tmp == '+') tmp++;
    
        int sum = 0;
        while(*tmp >= '0' && *tmp <= '9')
        {
            int val = *tmp - '0';
            if(sum > 0x7fffffff / 10 || (sum == 0x7fffffff / 10) && val > 0x7fffffff % 10)
                return flag ? 0x80000000 : 0x7fffffff;
    
            sum = sum * 10 + val;
            tmp++;
        }
    
        return flag ? -sum : sum;
    
    }
    
    
    int main()
    {
        char str[] = "   2147483649";
        cout << my_atoi(str) << endl;
        cout << str << endl;
        cout << 0x80000000 << endl; // 负数 符号位为1 以反码表示
        cout << 0x7fffffff << endl; // 整数 符号位为0 
        return 0;
    }
    

    参考:
    https://www.cnblogs.com/Manual-Linux/p/11773222.html

    strcmp

    #include <iostream>
    #include <string.h>
    #include <assert.h>
    using namespace std;
    
    int my_strcmp(const char* str1, const char* str2)
    {
        assert(str1 != NULL && str2 != NULL);
        while(str1 && str2 && *str1 == *str2)
        {
            str1++;
            str2++;
        }
        return *str1 - *str2;
    }
    
    int main()
    {
        char dst[] = "hello world";
        char src[] = " ok";
        cout << my_strcmp(dst, src) << endl;
        return 0;
    }
    
  • 相关阅读:
    2019-2020-1 20199329《Linux内核原理与分析》第十二周作业
    2019-2020-1 20199329《Linux内核原理与分析》第十一周作业
    2019-2020-1 20199329《Linux内核原理与分析》第九周作业
    2019-2020-1 20199329《Linux内核原理与分析》第八周作业
    PHP基础学习笔记5
    MYSQL内置函数【转】
    PHP基础学习笔记3
    常用端口大全【转】
    NMAP输出结果中CPE的含义【转】
    探测主机信息
  • 原文地址:https://www.cnblogs.com/Trevo/p/13517679.html
Copyright © 2020-2023  润新知