• 实现C语言字符串操作的库函数 包括基本的字符串复制 字符串长度 字符串比较等多种函数(C代码)


    头文件

    "mystring.h"

    #ifndef _MYSTR_H
    #define _MYSTR_H
    #include <stdio.h>
    #include <stdlib.h>
    
    /*复制*/
    char *mystrcpy(char *, const char *);                              //  [destin, source                       ]
    /*复制前n个*/
    char *mystrncpy(char *, const int, const char *);                  //  [distin, num, source                  ]
    /*求字符串串长度*/
    int mystrlen(const char *);                                        //  [str                                  ]
    /*字符在字符串中第一次出现的index*/
    int myindexof(const char *, const char *);                         //  [str, chr                             ]
    /*字符串在字符串中第一次出现的index*/
    int myindexofstr(const char *, const char *);                      //  [str, substr                          ]
    /*拼接两个字符串*/
    char *mystrcat(char *, const char *);                              //  [distin, source                       ]
    /*将后字符串的前n个拼接到前字符串末尾*/
    char *mystrncat(char *, const int, const char *);                  //  [distin, n, source                    ]
    /*字符在字符串中最后一次出现的index*/
    int mylastindexof(const char *, const char *);                     //  [str, chr                             ]
    /*反转字符串*/
    char *mystrrev(char *);                                            //  [str                                  ]
    /*字符串在字符串中最后一次出现的index*/
    int mylastindexofstr(const char *, const char *);                  //  [str, substr                          ]
    /*获得字符串从index开始到末尾的子串*/
    char *mysubstring(char *, const int, const char *);                //  [tosubstr, begin_index, str           ]
    /*获得字符串从f_index开始到t_index的子串*/
    char *myftsubstring(char *, const int, const int, const char *);   //  [tosubstr, begin_index, end_index, str]
    /*去除字符串头和串尾的空格(可处理多个)*/
    char *mytrimstr(char *);                                           //  [str                                  ]
    /*字符串比较(对应的字符ASCII值的比较)*/
    int mystrcmp(const char *, const char *);                          //  [str1, str2                           ]
    /*字符串的所有大写字符变小写*/
    char *mytolowerstr(char *);                                        //  [str                                  ]
    /*字符串的所有小写字符变大写*/
    char *mytoupperstr(char *);                                        //  [str                                  ]
    /*从字符串中获得指定index的字符*/
    char mygetchrfromstr(const int, const char *);                     //  [index, str                           ]
    /*以指定字符切割字符串*/
    int mystrsplit(char **, char *, const char);                       //  [distin, source, char lmt_chr         ]
    /*将字符串中全部字符设置为指定字符*/
    char *mystrset(char *, const char);                                //  [str, set_chr                         ]
    /*将字符串中前n个字符设置为指定字符*/
    char *mystrnset(char *, const int, const char);                    //  [str, num, set_chr                    ]
    /*忽略大小写进行字符串比较*/
    int mychricmp(const char, const char);                             //  [chr1, chr2                           ]
    /*忽略大小写进行字符串前n个字符的比较*/
    int mystrncmpi(const char *, const int, const char *);             //  [str1, num, str2                      ]
    /*修改字符串中全部指定的字符为新的字符*/
    char *mystrmodchr(char *, const char, const char);                 //  [str, old_chr, new_chr                ]
    /*修改字符串中全部指定的子字符串为新的字符串*/
    char *mystrmodstr(char *, const char *, const char *);             //  [str, old_str, new_str                ]
    /*复制字符串到安全的位置并返回指向它内存的指针*/
    char *mystrdup(const char *);                                      //  [source                               ]
    /*在字符串的指定index处插入字符*/
    char *mystrinsertchr(char *, const int, const char);               //  [str, index, chr                      ]
    /*在字符串的指定index处插入字符串*/
    char *mystrinsertstr(char *, const int, const char *);             //  [str, index, insert_str               ]
    /*数字字符串转int类型整数*/
    int mystrtoint(const char *);                                      //  [int_str                              ]
    /*数字字符串转double类型浮点数*/
    double mystrtodbl(const char *);                                   //  [dbl_str                              ]
    
    /////////////////////////////
    
    void test_mystrcpy();
    void test_mystrncpy();
    void test_mystrlen();
    void test_myindexof();
    void test_myindexofstr();
    void test_mystrcat();
    void test_mystrncat();
    void test_mylastindexof();
    void test_mystrrev();
    void test_mylastindexofstr();
    void test_mysubstring();
    void test_myftsubstring();
    void test_mytrimstr();
    void test_mystrcmp();
    void test_mytolowerstr();
    void test_mytoupperstr();
    void test_mygetchrfromstr();
    void test_mystrsplit();
    void test_mystrset();
    void test_mystrnset();
    void test_mychricmp();
    void test_mystrncmpi();
    void test_mystrmodchr();
    void test_mystrmodstr();
    void test_mystrdup();
    void test_mystrinsertchr();
    void test_mystrinsertstr();
    void test_mystrtoint();
    void test_mystrtodbl();
    #endif /* _MYSTR_H  */

    具体功能实现代码

    复制

    //返回值:成功正  失败NULL
    char *mystrcpy(char *destin, const char *source){
        if (!destin || !source){
            return NULL;
        }
        char *pd = destin;
        const char *ps = source;
        while ((*pd++ = *ps++))
            ;
        return destin;
    
    }

    复制前n个

    //返回值:成功正  失败NULL
    char *mystrncpy(char *destin, const int num, const char *source){
        if (!destin || !source){
            return NULL;
        }
        char *pd = destin;
        const char *ps = source;
        int i = 0;
        while ((i++ < num) && (*pd++ = *ps++))
            ;
        if (--i == num){
            return destin;
        }
        else{
            for(++i; i > -1; *pd-- = '', --i)
                ;
            return NULL;
        }
    }

    求字符串串长度

    int mystrlen(const char *str){
        if (!str){
            return -1;
        }
        const char *pstr = str;
        while(*pstr++)
            ;
        return (--pstr - str);
    }

    字符在字符串中第一次出现的index

    int myindexof(const char *str, const char *chr){
        if (!str || !chr ){
            return -1;
        }
        const char *pstr = str;
        const char *pchr = chr;
        char tmpc = '';
        while((tmpc = *pstr++) != *pchr && tmpc)
            ;
        if (!tmpc){
            return -1;
        }
        else{
            return (--pstr - str);
        }
    }

    字符串在字符串中第一次出现的index

    int myindexofstr(const char *str, const char *substr){
        if (!str || !substr){
            return -1;
        }
        const char *pstr = str;
        const char *psubstr = substr;
        int index = 0;
        while (*pstr){
            if (*psubstr == *pstr){
                ++pstr;
                if (*(++psubstr) == ''){
                    return index;
                }
            }
            else{
                pstr = pstr - (psubstr - substr) + 1;
                index = (pstr - str);
                psubstr = substr;
            }
        }
        return -1;
    }

    拼接两个字符串

    char *mystrcat(char *destin, const char *source){
        if (!destin || !source){
            return NULL;
        }
        char *pd = destin;
        const char *ps = source;
    
        while(*pd++);
        for(--pd; (*pd++ = *ps++);)
            ;
        return destin;
    }

    将后字符串的前n个拼接到前字符串末尾

    char *mystrncat(char *destin, const int n, const char *source){
        if(!destin || !source || n > mystrlen(source) || n < 0){
            return NULL;
        }
        char *pd = destin;
        const char *ps = source;
        pd += mystrlen(destin);
        for(int i = 0; i < n; ++i){
            *pd++ = *ps++;
        }
        *pd = '';
        return destin;
    }

    字符在字符串中最后一次出现的index

    int mylastindexof(const char *str, const char *chr){
        if(!str || !chr){
            return -1;
        }
        const char *pstr = str;
        const char *pchr = chr;
        pstr += mystrlen(str);
        while(*(--pstr) != *pchr)
            ;
        return (pstr - str);
    }

    反转字符串

    char *mystrrev(char *str){
        if(!str){
            return NULL;
        }
        int length = mystrlen(str);
        char *pstr = str;
        char *pend = str + (length - 1);
        for(int i = 0; i < (length / 2); ++i){
            static char tmp;
            tmp = *pstr;
            *pstr++ = *pend;
            *pend-- = tmp;
        }
        return str;
    }

    字符串在字符串中最后一次出现的index

    int mylastindexofstr(const char *str, const char *substr){
        if(!str || !substr){
            return -1;
        }
        const char *pstr = str;
        const char *psubstr = substr;
        int strlength = mystrlen(str);
        int sublength = mystrlen(substr);
        pstr += (strlength - 1);
        psubstr += (sublength - 1);
        int j_sub = 0;
        int endindex = strlength - 1;
        for(int i = 0; i < strlength; ++i){
            if(*pstr == *psubstr){
                --pstr;
                --psubstr;
                if(++j_sub == sublength){
                    return (endindex - sublength + 1);
                }
            }else{
                pstr += (j_sub - 1);
                psubstr = substr + sublength- 1;
                endindex = (pstr - str);
            }
        }
        return -1;
    }

    获得字符串从index开始到末尾的子串

    char *mysubstring(char *tosubstr, const int begin_index, const char *str){
        if(!tosubstr || !str || begin_index > 
           mystrlen(str) || begin_index < 0){
            return NULL;
        }
        char *ptosub = tosubstr;
        const char *pstr = str;
        pstr += begin_index;
        while((*ptosub++ = *pstr++))
            ;
        return tosubstr;
    }

    获得字符串从f_index开始到t_index的子串

    char *myftsubstring(char *tosubstr, const int begin_index,    //左闭右开
                        const int end_index, const char *str){
        if(!tosubstr || !str || begin_index >= end_index 
           || begin_index < 0 || end_index > mystrlen(str)){
            return NULL;
        }
        char *ptosub = tosubstr;
        const char *pstr = str;
        for((pstr += begin_index); pstr < (str + end_index); (*ptosub++ = *pstr++))
            ;
        *ptosub = '';
        return tosubstr;
    }

    去除字符串头和串尾的空格(可处理多个)

    char *mytrimstr(char *str){   //去除前后空格
        if(!str){
            return NULL;
        }
        char *pstr = str;
        char *p1 = str;
        char *p2 = str + (mystrlen(str) - 1);
        while(*p1++ == ' ')
            ;
        while(*p2-- == ' ')
            ;
        for((--p1, ++p2);p1 <= p2; (*pstr++ = *p1++))
            ;
        *pstr = '';
        return str;
    }

    字符串比较(对应的字符ASCII值的比较)

    int mystrcmp(const char *str1, const char *str2){
        if(!str1 || !str2){
            return -2;//-2表示没法比较
        }
        const char *pstr1 = str1;
        const char *pstr2 = str2;
        int flag = 0;
        while((*pstr1) && (*pstr2)){
            if(*pstr1 < *pstr2){
                flag = -1;
                break;
            }else if(*pstr1 > *pstr2){
                flag = 1;
                break;
            }
            ++pstr1;
            ++pstr2;
        }
        if(!(*pstr1) && !(*pstr2)){
            flag = 0;
        }else if(!(*pstr1)){
            flag = -1;
        }else if(!(*pstr2)){
            flag = 1;
        }
        return flag;
    }

    字符串的所有大写字符变小写

    char *mytolowerstr(char *str){
        if(!str){
            return NULL;
        }
        char *pstr = str;
        while(*pstr){
            if((*pstr >= 'A') && (*pstr <= 'Z')){
                *pstr += ('a' - 'A');
            }
            ++pstr;
        }
        return str;
    }

    字符串的所有小写字符变大写

    char *mytoupperstr(char *str){
        if(!str){
            return NULL;
        }
        char *pstr = str;
        while(*pstr){
            if((*pstr >= 'a') && (*pstr <= 'z')){
                *pstr -= ('a' - 'A');
            }
            ++pstr;
        }
        return str;
    }

    从字符串中获得指定index的字符

    char mygetchrfromstr(const int index, const char *str){
        if(!str || index < 0 || index >= mystrlen(str)){
            return '';
        }
        return *(str + index);
    }

    以指定字符切割字符串

    //将字符串按指定字符切割返回指向各个片段首字符的指针  返回子串个数/失败-1
    int mystrsplit(char **destin, char *source, const char lmt_chr){
        if(!destin || !source || !lmt_chr){
            return -1;
        }
        char **pd = destin;
        char *ps = source;
        int flag = 0;
        int sub_num = 0;
        while(*ps){
            if(*ps != lmt_chr){
                if(!flag){
                    *pd++ = ps;
                    ++sub_num;
                }
                flag = 1;
            }else{
                *ps = '';
                flag = 0;
            }
            ++ps;
        }
        return sub_num;
    }

    将字符串中全部字符设置为指定字符

    char *mystrset(char *str, const char set_chr){
        if(!str || !set_chr){
            return NULL;
        }
        char *pstr = str;
        for(; *pstr; (*pstr++ = set_chr))
            ;
        return str;
    }

    将字符串中前n个字符设置为指定字符

    char *mystrnset(char *str, const int num, const char set_chr){
        if(!str || !set_chr || num < 0 || num > mystrlen(str)){
            return NULL;
        }
        char *pstr = str;
        for(int i = 0; i < num; (*pstr++ = set_chr), ++i)
            ;
        return str;
    }

    忽略大小写进行字符串比较

    int mychricmp(const char chr1, const char chr2){
        if(!chr1 || !chr2){
            return -2;
        }
        int diff = chr1 - chr2;
        if(diff == 0 || (diff == ('a' - 'A')) || (diff == ('A' - 'a'))){
            return 0;
        }else if(diff < 0){
            return -1;
        }else{
            return 1;
        }
    }

    忽略大小写进行字符串前n个字符的比较

    int mystrncmpi(const char *str1, const int num, const char *str2){
        if(!str1 || !str2 || num <= 0 ||
            num > mystrlen(str1) || num > mystrlen(str2)){
           return -2;
        }
        const char *pstr1 = str1;
        const char *pstr2 = str2;
        for(int i = 0; i < num; ++i){
            int flag = mychricmp(*pstr1++, *pstr2++);
            if(flag == -1){
                return -1;
            }else if(flag == 1){
                return 1;
            }else if(flag == -2){
                return -2; //失败
            }
        }
        return 0;
    
    }

    修改字符串中全部指定的字符为新的字符

    char *mystrmodchr(char *str, const char old_chr, const char new_chr){
        if(!str || !old_chr){   //支持换成''
            return NULL;
        }
        char *pstr = str;
        while(*pstr){
            if(*pstr == old_chr){
                *pstr = new_chr;
            }
            ++pstr;
        }
        return str;
    }

    修改字符串中全部指定的子字符串为新的字符串

    char *mystrmodstr(char *str, const char *old_str,const char *new_str){
        if(!str || !old_str || !new_str){
            return NULL;
        }
        char *pstr = str;
        int index = 0;
        while((index = myindexofstr(pstr, old_str)) != -1){
            const char *pnew_str = new_str;
            for(pstr += index; *pnew_str; *pstr++ = *pnew_str++)
                ;
        }
        return str;
    }

    复制字符串到安全的位置并返回指向它内存的指针

    char *mystrdup(const char *source){  //在堆中申请的内存  用时注意 free
        if(!source){
            return NULL;
        }
        int str_length = mystrlen(source);
        char *destin = NULL;
        if(!(destin = (char *)calloc((str_length + 1), sizeof(char)))){
            return NULL;
        }
        if(!(mystrcpy(destin, source))){
            return NULL;
        }
        return destin;
    }

    在字符串的指定index处插入字符

    char *mystrinsertchr(char *str, const int index, const char chr){
        int str_length = mystrlen(str);
        if(!str || index < 0 || index > str_length){ //支持插入''  允许插在串尾
            return NULL;
        }
        char *pstr = str, *lastp;
        pstr += str_length;
        lastp = pstr + 1;
        for(int i = 0; i <= (str_length - index); (*lastp-- = *pstr--), ++i)
            ;
        *(++pstr) = chr;
        return str;
    }

    在字符串的指定index处插入字符串

    char *mystrinsertstr(char *str, const int index, const char *insert_str){
        int str_length = mystrlen(str);
        if(!str || !insert_str || index < 0 || index > str_length){ //允许插在串尾
            return NULL;
        }
        int insert_str_length = mystrlen(insert_str);
        char *pstr = str, *lastp;
        const char *pinsert_str = insert_str;
        pstr += str_length;
        lastp = pstr + insert_str_length;
        for(int i = 0; i <= (str_length - index); (*lastp-- = *pstr--), ++i)
            ;
        for(int i = 0; i < insert_str_length; (*(++pstr) = *pinsert_str++), ++i)
            ;
        return str;
    }

    数字字符串转int类型整数

    int mystrtoint(const char *int_str){
        if(!int_str){
             fprintf(stderr, "error: input str pointer is null
    ");
             return 1;
        }
        const char *pint_str = int_str;
        for(; *pint_str == ' ' || *pint_str == '	' ||
         *pint_str == '
    ' || *pint_str == '
    '; ++pint_str)  //跳过前面的空格、制表符、换行符
            ;
        int sign = 1;
        if(*pint_str == '-' || *pint_str == '+'){
            *pint_str == '-' ? (sign = -1) : (sign = 1);
            ++pint_str;
        }
        int the_intnum = 0;
        //没做英文字符的处理   那显然不是纯数字的字符串
        for(; *pint_str; (the_intnum = (*pint_str - '0') + 10 * the_intnum), ++pint_str)
            ;
        return (sign * the_intnum);
    }

    数字字符串转double类型浮点数

    double mystrtodbl(const char *dbl_str){
        if(!dbl_str){
            fprintf(stderr, "error: input str pointer is null
    ");
            return 1;
        }
        const char *pdbl_str = dbl_str;
        for(; *pdbl_str == ' ' || *pdbl_str == '	' ||
         *pdbl_str == '
    ' || *pdbl_str == '
    '; ++pdbl_str)  //跳过前面的空格、制表符、换行符
            ;
        double sign = 1.0;
        if(*pdbl_str == '-' || *pdbl_str == '+'){
            *pdbl_str == '-' ? (sign = -1.0) : (sign = 1.0);
            ++pdbl_str;
        }
        double num_bef_point = 0.0;
        double num_aft_point = 0.0;
        double num_double = 0.0;
        for(; *pdbl_str != '.' && *pdbl_str; ++pdbl_str){
            num_bef_point = (*pdbl_str - '0') + 10.0 * num_bef_point;
        }
        if(!(*pdbl_str)){;
            num_double = sign * num_bef_point;
        }else{
            double point_flag = 0.1;
            for(++pdbl_str; *pdbl_str; ++pdbl_str){
                num_aft_point += (*pdbl_str - '0') * point_flag;
                point_flag *= 0.1;
            }
            num_double = sign * (num_bef_point + num_aft_point);
        }
        return num_double;
    }

    测试功能代码(使用样例)

    复制

    void test_mystrcpy(){
        printf("
    >>>char *mystrcpy(char *, const char *)
    ");
        char str[64] = {0};
        printf("str:[ %s ]
    ", str);
        printf("soc:[ %s ]
    ", "hello world");
        mystrcpy(str, "hello world");
        printf("str_op:[ %s ]
    ", str);
    }

    复制前n个

    void test_mystrncpy(){
        printf("
    >>>char *mystrncpy(char *, const int, const char *)
    ");
        char str[64] = {0};
        printf("str:[ %s ]
    ", str);
        printf("soc:[ %s ]
    ", "hello world");
        printf("num:[ %d ]
    ", 5);
        mystrncpy(str,5, "hello world");
        printf("str_op:[ %s ]
    ", str);
    }

    求字符串串长度

    void test_mystrlen(){
        printf("
    >>>int mystrlen(const char *)
    ");
        char *p = "hello";
        printf("str:[ %s ]
    ", p);
        printf("str_op:[ %d ]
    ", mystrlen(p));
    }

    字符在字符串中第一次出现的index

    void test_myindexof(){
        printf("
    >>>int myindexof(const char *, const char *)
    ");
        char *p = "aBcaBc";
        char c = 'B';
        printf("str:[ %s ]
    ", p);
        printf("chr:[ %c ]
    ", c);
        printf("str_op:[ %d ]
    ", myindexof(p, &c));
    }

    字符串在字符串中第一次出现的index

    void test_myindexofstr(){
        printf("
    >>>int myindexofstr(const char *, const char *)
    ");
        char *p1 = "abCDefghCDij";
        char *p2 = "CD";
        printf("str:[ %s ]
    ", p1);
        printf("str:[ %s ]
    ", p2);
        printf("str_op:[ %d ]
    ", myindexofstr(p1, p2));
    }

    拼接两个字符串

    void test_mystrcat(){
        printf("
    >>>char *mystrcat(char *, const char *)
    ");
        char str[64] = {'h', 'e', 'l', 'l', 'o', ' '};
        char *p = "world";
        printf("str1:[ %s ]
    ", str);
        printf("str2:[ %s ]
    ", p);
        mystrcat(str, p);
        printf("str_op:[ %s ]
    ", str);
    }

    将后字符串的前n个拼接到前字符串末尾

    void test_mystrncat(){
        printf("
    >>>char *mystrncat(char *, const int, const char *)
    ");
        char str[64] = {'h', 'e', 'l', 'l', 'o', ' '};
        char *p = "world";
        printf("str1:[ %s ]
    ", str);
        printf("str2:[ %s ]
    ", p);
        printf("num:[ %d ]
    ", 3);
        mystrncat(str, 3, p);
        printf("str_op:[ %s ]
    ", str);
    }

    字符在字符串中最后一次出现的index

    void test_mylastindexof(){
        printf("
    >>>int mylastindexof(const char *, const char *)
    ");
        char *p = "aBcaBc";
        char c = 'B';
        printf("str:[ %s ]
    ", p);
        printf("chr:[ %c ]
    ", c);
        printf("str_op:[ %d ]
    ", mylastindexof(p, &c));
    }

    反转字符串

    void test_mystrrev(){
        printf("
    >>>char *mystrrev(char *)
    ");
        char str[64] = {'h', 'e', 'l', 'l', 'o', ' '};
        printf("str:[ %s ]
    ", str);
        mystrrev(str);
        printf("str_op:[ %s ]
    ", str);
    }

    字符串在字符串中最后一次出现的index

    void test_mylastindexofstr(){
        printf("
    >>>int mylastindexofstr(const char *, const char *)
    ");
        char *p1 = "abCDefghCDij";
        char *p2 = "CD";
        printf("str1:[ %s ]
    ", p1);
        printf("str2:[ %s ]
    ", p2);
        printf("str_op:[ %d ]
    ", mylastindexofstr(p1, p2));
    }

    获得字符串从index开始到末尾的子串

    void test_mysubstring(){
        printf("
    >>>char *mysubstring(char *, const int, const char *)
    ");
        char str[] = {0};
        char *p = "hello";
        printf("str1:[ %s ]
    ", str);
        printf("str2:[ %s ]
    ", p);
        printf("index:[ %d ]
    ", 3);
        mysubstring(str, 3, p);
        printf("str_op:[ %s ]
    ", str);
    }

    获得字符串从f_index开始到t_index的子串

    void test_myftsubstring(){
        printf("
    >>>char *myftsubstring(char *, const int, const int, const char *)
    ");
        char str[] = {0};
        char *p = "hello world";
        printf("str1:[ %s ]
    ", str);
        printf("str2:[ %s ]
    ", p);
        printf("from:[ %d ]
    ", 3);
        printf("to:[ %d ]
    ", 8);
        myftsubstring(str, 3, 8, p);
        printf("str_op:[ %s ]
    ", str);
    }

    去除字符串头和串尾的空格(可处理多个)

    void test_mytrimstr(){
        printf("
    >>>char *mytrimstr(char *)
    ");
        char str[] = {' ', ' ', 'h', 'e',  'l', 'l', 'o', ' ', ' '};
        printf("str:[ %s ]
    ", str);
        mytrimstr(str);
        printf("str_op:[ %s ]
    ", str);
    }

    字符串比较(对应的字符ASCII值的比较)

    void test_mystrcmp(){
        printf("
    >>>int mystrcmp(const char *, const char *)
    ");
        char *p1 = "abcd";
        char *p2 = "aBdc";
        printf("str1:[ %s ]
    ", p1);
        printf("str2:[ %s ]
    ", p2);
        printf("str_op:[ %d ]
    ", mystrcmp(p1, p2));
    }

    字符串的所有大写字符变小写

    void test_mytolowerstr(){
        printf("
    >>>char *mytolowerstr(char *)
    ");
        char str[64] = {'a', 'b', 'C', 'D', 'e'};
        printf("str:[ %s ]
    ", str);
        mytolowerstr(str);
        printf("str_op:[ %s ]
    ", str);
    }

    字符串的所有小写字符变大写

    void test_mytoupperstr(){
        printf("
    >>>char *mytoupperstr(char *)
    ");
        char str[64] = {'a', 'b', 'C', 'D', 'e'};
        printf("str:[ %s ]
    ", str);
        mytoupperstr(str);
        printf("str_op:[ %s ]
    ", str);
    }

    从字符串中获得指定index的字符

    void test_mygetchrfromstr(){
        printf("
    >>>char mygetchrfromstr(const int, const char *)
    ");
        char *p = "hello";
        printf("str:[ %s ]
    ", p);
        printf("index:[ %d ]
    ", 3);
        printf("str_op:[ %c ]
    ", mygetchrfromstr(3, p));
    }

    以指定字符切割字符串

    void test_mystrsplit(){
        printf("
    >>>int mystrsplit(char **, char *, const char)
    ");
        char *p[10] = {0};
        char **p1 = p;
        char str[64] = {0};
        mystrcpy(str, "  ab cd   ef GH ");
        printf("str:[ %s ]
    ", str);
        int num = mystrsplit(p, str, ' '); //ÒÔ¿Õ¸ñÇиî
        for(int i = 0; i < num; ++i){
            printf("str_op:[ %s ]
    ", *p1++);
        }
    }

    将字符串中全部字符设置为指定字符

    void test_mystrset(){
        printf("
    >>>char *mystrset(char *, const char)
    ");
        char str[64] = {'h', 'e',  'l', 'l', 'o'};
        printf("str:[ %s ]
    ", str);
        printf("chr:[ %c ]
    ", 'A');
        mystrset(str, 'A');
        printf("str_op:[ %s ]
    ", str);
    }

    将字符串中前n个字符设置为指定字符

    void test_mystrnset(){
        printf("
    >>>char *mystrnset(char *, const int, const char)
    ");
        char str[64] = {'h', 'e',  'l', 'l', 'o'};
        printf("str:[ %s ]
    ", str);
        printf("chr:[ %c ]
    ", 'A');
        printf("num:[ %d ]
    ", 3);
        mystrnset(str, 3, 'A');
        printf("str_op:[ %s ]
    ", str);
    }

    忽略大小写进行字符串比较

    void test_mychricmp(){
        printf("
    >>>int mychricmp(const char, const char)
    ");
        char c1 = 'a';
        char c2 = 'A';
        printf("chr1:[ %c ]
    ", c1);
        printf("chr2:[ %c ]
    ", c2);
        printf("str_op:[ %d ]
    ", mychricmp(c1, c2));
    }

    忽略大小写进行字符串前n个字符的比较

    void test_mystrncmpi(){
        printf("
    >>>int mystrncmpi(const char *, const int, const char *)
    ");
        char *p1 = "AAAbc";
        char *p2 = "aaaBC";
        printf("str1:[ %s ]
    ", p1);
        printf("str2:[ %s ]
    ", p2);
        printf("num:[ %d ]
    ", 3);
        printf("str_op:[ %d ]
    ", mystrncmpi(p1, 3, p2));
    }

    修改字符串中全部指定的字符为新的字符

    void test_mystrmodchr(){
        printf("
    >>>char *mystrmodchr(char *, const char, const char)
    ");
        char str[64] = {'a', 'b', 'D', 'c', 'D', 'E'};
        printf("str:[ %s ]
    ", str);
        printf("oldchr:[ %c ]
    ", 'D');
        printf("newchr:[ %c ]
    ", 'W');
        mystrmodchr(str, 'D', 'W');
        printf("str_op:[ %s ]
    ", str);
    }

    修改字符串中全部指定的子字符串为新的字符串

    void test_mystrmodstr(){
        printf("
    >>>char *mystrmodstr(char *, const char *, const char *)
    ");
        char str[64] = {0};
        mystrcpy(str, "abCDEefCDErgfCDE");
        printf("str:[ %s ]
    ", str);
        char *p1 = "CDE";
        char *p2 = "HHH";
        printf("oldstr:[ %s ]
    ", p1);
        printf("newstr:[ %s ]
    ", p2);
        mystrmodstr(str, p1, p2);
        printf("str_op:[ %s ]
    ", str);
    }

    复制字符串到安全的位置并返回指向它内存的指针

    void test_mystrdup(){
        printf("
    >>>char *mystrdup(const char *)
    ");
        char *p1 = "hello", *p2 = NULL;
        printf("str1:[ %s ]
    ", p2);
        printf("str2:[ %s ]
    ", p1);
        p2 = mystrdup(p1);
        printf("str_op:[ %s ]
    ", p2);
        free(p2);
        p2 = NULL;
    }

    在字符串的指定index处插入字符

    void test_mystrinsertchr(){
        printf("
    >>>char *mystrinsertchr(char *, const int, const char)
    ");
        char str[64] = {'h', 'e',  'l', 'l', 'o'};
        printf("str:[ %s ]
    ", str);
        printf("index:[ %d ]
    ", 2);
        mystrinsertchr(str, 2, 'W');
        printf("str_op:[ %s ]
    ", str);
    }

    在字符串的指定index处插入字符串

    void test_mystrinsertstr(){
        printf("
    >>>char *mystrinsertstr(char *, const int, const char *)
    ");
        char str[64] = {'h', 'e',  'l', 'l', 'o'};
        printf("str:[ %s ]
    ", str);
        printf("index:[ %d ]
    ", 2);
        char *p = "QQQ";
        mystrinsertstr(str, 2, p);
        printf("str_op:[ %s ]
    ", str);
    }

    数字字符串转int类型整数

    void test_mystrtoint(){
        printf("
    >>>int mystrtoint(const char *)
    ");
        char *p = "     +1034";
        int num = 0;
        printf("str:[ %s ]
    ", p);
        printf("num:[ %d ]
    ", num);
        num = mystrtoint(p);
        printf("str_op:[ %d ]
    ", num);
    }

    数字字符串转double类型浮点数

    void test_mystrtodbl(){
        printf("
    >>>double mystrtodbl(const char *)
    ");
        char *p = "     +1034.66";
        double num = 0;
        printf("str:[ %s ]
    ", p);
        printf("num:[ %lf ]
    ", num);
        num = mystrtodbl(p);
        printf("str_op:[ %lf ]
    ", num);
    }

    整体测试

    main.c

    #include "mystring.h"
    
    int main()
    {
        printf("__________________TEST_MYSTR_BY_XLC___________________
    ");
        test_mystrcpy();
        test_mystrncpy();
        test_mystrlen();
        test_myindexof();
        test_myindexofstr();
        test_mystrcat();
        test_mystrncat();
        test_mylastindexof();
        test_mystrrev();
        test_mylastindexofstr();
        test_mysubstring();
        test_myftsubstring();
        test_mytrimstr();
        test_mystrcmp();
        test_mytolowerstr();
        test_mytoupperstr();
        test_mygetchrfromstr();
        test_mystrsplit();
        test_mystrset();
        test_mystrnset();
        test_mychricmp();
        test_mystrncmpi();
        test_mystrmodchr();
        test_mystrmodstr();
        test_mystrdup();
        test_mystrinsertchr();
        test_mystrinsertstr();
        test_mystrtoint();
        test_mystrtodbl();
        printf("
    -------------------------------------------------------
    ");
        return 0;
    }

    结果 

    __________________TEST_MYSTR_BY_XLC___________________
    
    >>>char *mystrcpy(char *, const char *)
    str:[  ]
    soc:[ hello world ]
    str_op:[ hello world ]
    
    >>>char *mystrncpy(char *, const int, const char *)
    str:[  ]
    soc:[ hello world ]
    num:[ 5 ]
    str_op:[ hello ]
    
    >>>int mystrlen(const char *)
    str:[ hello ]
    str_op:[ 5 ]
    
    >>>int myindexof(const char *, const char *)
    str:[ aBcaBc ]
    chr:[ B ]
    str_op:[ 1 ]
    
    >>>int myindexofstr(const char *, const char *)
    str:[ abCDefghCDij ]
    str:[ CD ]
    str_op:[ 2 ]
    
    >>>char *mystrcat(char *, const char *)
    str1:[ hello  ]
    str2:[ world ]
    str_op:[ hello world ]
    
    >>>char *mystrncat(char *, const int, const char *)
    str1:[ hello  ]
    str2:[ world ]
    num:[ 3 ]
    str_op:[ hello wor ]
    
    >>>int mylastindexof(const char *, const char *)
    str:[ aBcaBc ]
    chr:[ B ]
    str_op:[ 4 ]
    
    >>>char *mystrrev(char *)
    str:[ hello  ]
    str_op:[  olleh ]
    
    >>>int mylastindexofstr(const char *, const char *)
    str1:[ abCDefghCDij ]
    str2:[ CD ]
    str_op:[ 8 ]
    
    >>>char *mysubstring(char *, const int, const char *)
    str1:[  ]
    str2:[ hello ]
    index:[ 3 ]
    str_op:[ lo ]
    
    >>>char *myftsubstring(char *, const int, const int, const char *)
    str1:[  ]
    str2:[ hello world ]
    from:[ 3 ]
    to:[ 8 ]
    str_op:[ lo wo ]
    
    >>>char *mytrimstr(char *)
    str:[   hello   ]
    str_op:[ hello ]
    
    >>>int mystrcmp(const char *, const char *)
    str1:[ abcd ]
    str2:[ aBdc ]
    str_op:[ 1 ]
    
    >>>char *mytolowerstr(char *)
    str:[ abCDe ]
    str_op:[ abcde ]
    
    >>>char *mytoupperstr(char *)
    str:[ abCDe ]
    str_op:[ ABCDE ]
    
    >>>char mygetchrfromstr(const int, const char *)
    str:[ hello ]
    index:[ 3 ]
    str_op:[ l ]
    
    >>>int mystrsplit(char **, char *, const char)
    str:[   ab cd   ef GH  ]
    str_op:[ ab ]
    str_op:[ cd ]
    str_op:[ ef ]
    str_op:[ GH ]
    
    >>>char *mystrset(char *, const char)
    str:[ hello ]
    chr:[ A ]
    str_op:[ AAAAA ]
    
    >>>char *mystrnset(char *, const int, const char)
    str:[ hello ]
    chr:[ A ]
    num:[ 3 ]
    str_op:[ AAAlo ]
    
    >>>int mychricmp(const char, const char)
    chr1:[ a ]
    chr2:[ A ]
    str_op:[ 0 ]
    
    >>>int mystrncmpi(const char *, const int, const char *)
    str1:[ AAAbc ]
    str2:[ aaaBC ]
    num:[ 3 ]
    str_op:[ 0 ]
    
    >>>char *mystrmodchr(char *, const char, const char)
    str:[ abDcDE ]
    oldchr:[ D ]
    newchr:[ W ]
    str_op:[ abWcWE ]
    
    >>>char *mystrmodstr(char *, const char *, const char *)
    str:[ abCDEefCDErgfCDE ]
    oldstr:[ CDE ]
    newstr:[ HHH ]
    str_op:[ abHHHefHHHrgfHHH ]
    
    >>>char *mystrdup(const char *)
    str1:[ (null) ]
    str2:[ hello ]
    str_op:[ hello ]
    
    >>>char *mystrinsertchr(char *, const int, const char)
    str:[ hello ]
    index:[ 2 ]
    str_op:[ heWllo ]
    
    >>>char *mystrinsertstr(char *, const int, const char *)
    str:[ hello ]
    index:[ 2 ]
    str_op:[ heQQQllo ]
    
    >>>int mystrtoint(const char *)
    str:[      +1034 ]
    num:[ 0 ]
    str_op:[ 1034 ]
    
    >>>double mystrtodbl(const char *)
    str:[      +1034.66 ]
    num:[ 0.000000 ]
    str_op:[ 1034.660000 ]
    
    -------------------------------------------------------
  • 相关阅读:
    Ratchet(WebSockets for PHP)的官方TUTORIALS 的实践
    TCP_Wrappers访问控制
    PAM认证机制
    AIDE入侵检测系统
    使用gpg来加密数据
    openssl数据加密
    搭建私有CA
    进程概念
    进程管理命令
    Linux的信号管理
  • 原文地址:https://www.cnblogs.com/xinglichao/p/9158967.html
Copyright © 2020-2023  润新知