你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / 数据库开发
自己实现的C语言的字符串替换函数
 
   C语言中没有提供字符串替换函数,网上能找到的类似函数也只是能替换一个,不能替换全部,工作中却常常要用到这个功能,故实现一个函数。该函数所使用到的相关函数均是自己实现,没有调用库函数。
         相关代码如下:
 
[html]  
/********************************************************************  
*  Function:   my_strstr()  
*  Description: 在一个字符串中查找一个子串;  
*  Calls:       无;  
*  Called By:   无  
*  Input:      ps: 源;       pd:子串  
*  Output:     无;  
*  Return :     0:源字符串中没有子串; 1:源字符串中有子串;  
*  Author:      ChenZhiFa  
*  Others:      无;  
*  date of completion:   
*********************************************************************/  
char * my_strstr(char * ps,char *pd)  
{  
    char *pt = pd;  
    int c = 0;  
  
    while(*ps != '\0')  
    {  
        if(*ps == *pd)  
        {  
              
            while(*ps == *pd && *pd!='\0')  
            {  
                ps++;  
                pd++;  
                c++;  
            }  
        }  
  
        else  
        {  
            ps++;  
  
        }  
  
        if(*pd == '\0')  
        {  
            //sum++;  
            return (ps - c);  
        }  
            c = 0;  
            pd = pt;  
    }  
  
    return 0;  
}  
  
/********************************************************************  
*  Function:   memcpy()  
*  Description: 复制一个内存区域到另一个区域;  
*  Calls:       无;  
*  Called By:   无  
*  Input:       src: 源;  
                 count: 复制字节数.  
*  Output:      dest: 复制目的地;  
*  Return :      dest;  
*  Author:       ChenZhiFa  
*  Others:       无;  
*  date of completion:   
*********************************************************************/  
void * memcpy(void * dest,const void *src,size_t count)  
{  
    char *tmp = (char *) dest, *s = (char *) src;  
  
    while (count--)  
        *tmp++ = *s++;  
  
    return dest;  
}  
  
/********************************************************************  
*  Function:   str_replace()  
*  Description: 在一个字符串中查找一个子串,并且把所有符合的子串用  
                另一个替换字符串替换。  
*  Calls:        memcpy();  
*  Called By:    无  
*  Input:       p_source:要查找的母字符串; p_seach要查找的子字符串;  
                 p_repstr:替换的字符串;  
*  Output:      p_result:存放结果;  
*  Return :      返回替换成功的子串数量;  
*  Author:       ChenZhiFa  
*  Others:       p_result要足够大的空间存放结果,所以输入参数都要以\0结束;  
*  date of completion:   
*********************************************************************/  
int str_replace(char *p_result,char* p_source,char* p_seach,char *p_repstr)  
{  
  
    int c = 0;  
    int repstr_leng = 0;  
    int searchstr_leng = 0;  
  
    char *p1;  
    char *presult = p_result;  
    char *psource = p_source;  
    char *prep = p_repstr;  
    char *pseach = p_seach;  
    int nLen = 0;   
  
    repstr_leng = strlen(prep);  
    searchstr_leng = strlen(pseach);  
  
    do{   
        p1 = my_strstr(psource,p_seach);  
  
        if (p1 == 0)  
        {  
            strcpy(presult,psource);  
            return c;  
        }  
  
        c++;  //匹配子串计数加1;  
        printf("结果:%s\r\n",p_result);  
        printf("源字符:%s\r\n",p_source);  
  
        // 拷贝上一个替换点和下一个替换点中间的字符串  
        nLen = p1 - psource;  
        memcpy(presult, psource, nLen);  
  
        // 拷贝需要替换的字符串  
        memcpy(presult + nLen,p_repstr,repstr_leng);  
  
        psource = p1 + searchstr_leng;  
        presult = presult + nLen + repstr_leng;  
  
    }while(p1);  
  
    return c;  
  
}  
测试代码如下:
 
 
[cpp]  
#define MAX 200  
int main(void)  
{  
    int i = 0;  
  
    char s[MAX] ={0};         //存放源字串  
    char s1[MAX]={0};         //存放子字串  
    char s2[MAX]={0};         //存放替换字串  
    char result_a[2000] = {0};//存放替换结果;  
  
    char *p,*ptm,*pr;  
  
    puts("Please input the string for s:");  
    scanf("%s",s);  
  
    puts("Please input the string for s1:");  
    scanf("%s",s1);  
  
    puts("Please input the string for s2:");  
    scanf("%s",s2);  
  
    ptm = s;  
    pr = result_a;  
  
    i = str_replace(pr,ptm,s1,s2);  
  
    printf("替换%d个子字符串;\r\n",i);   
    printf("替换后结果:%s\r\n",result_a);  
  
    system("pause");  
}  
 
运行结果如果:
[cpp]  
Please input the string for s:  
123123123123  
Please input the string for s1:  
23  
Please input the string for s2:  
abcdefg  
结果:  www.2cto.com
源字符:123123123123  
结果:1abcdefg  
源字符:123123123123  
结果:1abcdefg1abcdefg  
源字符:123123123123  
结果:1abcdefg1abcdefg1abcdefg  
源字符:123123123123  
替换4个子字符串;  
替换后结果:1abcdefg1abcdefg1abcdefg1abcdefg  
请按任意键继续. . .  
  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089