zoukankan      html  css  js  c++  java
  • C 语言实现字符串替换

    #include <stdio.h>
    #include <string.h>
    
    
    typedef void VOID;
    typedef char *  PSTR;
    typedef int INT;
    typedef char STR;
    
    /***********************************************
     *  * StringReplace
     *   * 字符串替换
     *    *
     *     * pszInput       要转变的字符串
     *      * pszOld                要替换的子串
     *       * pszNew               被替换成的子串
     *        * pszOutput   输出的缓冲区
     *         * nOutputlen 输出缓冲区的长度
     *          *
     *           **********************************************/
    VOID StringReplace(PSTR pszInput, PSTR pszOld, PSTR pszNew,PSTR pszOutput,INT nOutputlen)
    {
            INT nLen=0;
            STR *s, *p;
            s = pszInput;
            while (s != NULL)
            {
                    p = strstr(s, pszOld);
    
                    if (p == NULL )
                    {
                            memcpy(pszOutput+nLen,s,strlen(s)+nLen>nOutputlen?nOutputlen-nLen:strlen(s));
                            return ;
                    }
                    memcpy(pszOutput+nLen,s,p-s+nLen>nOutputlen?nOutputlen-nLen:p-s);
                    nLen+=p-s+nLen>nOutputlen?nOutputlen-nLen:p-s;
                    if(nLen>=nOutputlen)
                    {
                            return;
                    }
                    memcpy(pszOutput+nLen,pszNew,strlen(pszNew)+nLen>nOutputlen?nOutputlen-nLen:strlen(pszNew));
                    nLen+=strlen(pszNew)+nLen>nOutputlen?nOutputlen-nLen:strlen(pszNew);
                    if(nLen>=nOutputlen)
                    {
                            return;
                    }
                    s+=strlen(pszOld)+p-s;
            }
            return ;
    }
    int main(){
    
        char str1_t[10] = "111";
        char str2_t[3] = "11";
        char str3_t[2] = "1";
        char strout_t[10];
        StringReplace(str1_t,str2_t,str3_t,strout_t,10);
        printf("%s",strout_t);
        return 0;
    }
  • 相关阅读:
    pytorch_基于cifar创建自己的数据集并训练
    Pytorch_3.8_多层感知机
    Pytorch_3.6_ SOFTMAX回归的从零实现
    Linux(debian)下的Python程序守护进程
    Ubuntu16.04安装OpenCV3.4.3
    Beaglebone black 安装docker
    电脑与虚拟机ping
    Beaglebone升级Python3.7过程
    多图上传预览
    放大镜代码
  • 原文地址:https://www.cnblogs.com/dpf-10/p/5952267.html
Copyright © 2011-2022 走看看