Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5401881
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2009-02-09 22:17:54

 

/*
*    定义函数 char * StringReplace(char* _pDest, const char* _pSrc, const char* _pKey, const char* _pReplace);
*    表头文件 #include
*    函数描述    字符串替换函数, 在字符串 _pSrc 中查找 _pKey 串,并将其替换成 _pReplace 串, 将结果保存在_pDest中
*                        _pSrc 要查找的原串
*                        _pKey 要查找的关键串
*                        _pReplace 要替换的内容
*                        _pDest 保存处理后的字符串,要有足够的空间来容纳处理后的字符串
*                        
*     返回值     返回 _pDest    的字符串起始地址。
*        作 者 武立强
*        时    间    2009-02-08
*        注    意    就两种情况,--匹配成功,---匹配失败
*                        StringReplace(strDest, strSrc, " ", ""); ---实现过滤字符串空格的作用
*/

char * StringReplace(char* _pDest, const char* _pSrc, const char* _pKey, const char* _pReplace)
{
    assert( NULL != _pDest && NULL != _pSrc && NULL != _pKey && NULL != _pReplace );
    
    // const char * 不能通过指针改变所指向的字符串的值

    const char* pKeyFirst = NULL;
    const char* pFirstFind = NULL; // 标记找到的第一个字符的位置

    const char* pLastFind = NULL;    //标记找到的最后一个字符的位置

    const char* pReplaceFirst = NULL;
    char* pDestFirst = NULL;

    int nFirstFind = 0 ; //第一个字符是否找到, 0--没找到, 1--找到

    int nLastFind = 0;    // 最后一个字符

    
    pKeyFirst = _pKey;
    pReplaceFirst = _pReplace;
    pDestFirst = _pDest;

    while( '\0' != *_pSrc )
    {
        // 逻辑比较部分, 确保完全匹配查找关键串

        if( 0 == nFirstFind && *_pSrc == *_pKey )
        {
            nFirstFind = 1;
            pFirstFind = _pSrc;
            _pKey++;
        }
        // 匹配成功

        else if( 1 == nFirstFind && *_pSrc == *_pKey && '\0' == *(_pKey+1) )
        {
            nLastFind = 1;
            pLastFind = _pSrc;
            _pKey = pKeyFirst;
        }
        else if( 1 == nFirstFind && *_pSrc == *_pKey )
        {
            _pKey++;
        }    
        // 部分匹配,查找失败,要进行补救----- 将第一个字符移过去,从第二个字符开始从新匹配.

        else if( 1 == nFirstFind && *_pSrc != *_pKey )
        {    
            *_pDest++ = *pFirstFind++;
            _pSrc = pFirstFind;
            nFirstFind = 0;
            pFirstFind = NULL;
            _pKey = pKeyFirst;
        }
                
        // 找到,替换为目标串

        if( 1 == nFirstFind && 1 == nLastFind )
        {
            while( '\0' != *_pReplace )
            {
                *_pDest++ = *_pReplace++;
            }

            nFirstFind = 0;
            nLastFind = 0;
            _pReplace = pReplaceFirst;
            _pKey = pKeyFirst;
        }
        // 没找到

        else if( 0 == nFirstFind && 0 == nLastFind )
        {
            *_pDest = *_pSrc;
            _pDest++;
        }
        // 针对一个字符替换为另一个字符的情况

        else if( 1 == nFirstFind && '\0' == *(_pKey) )
        {
            while( '\0' != *_pReplace )
            {
                *_pDest++ = *_pReplace++;
            }

            nFirstFind = 0;
            nLastFind = 0;
            _pReplace = pReplaceFirst;
            _pKey = pKeyFirst;
        }
        // 最后一次循环了,还没完成---匹配失败

        else if( 1 == nFirstFind && 0 == nLastFind && '\0' == *(_pSrc + 1) )
        {
            for(int i=0; i<=(_pSrc - pFirstFind+1); i++ )
            {
                *_pDest++ = *pFirstFind++;
            }
            nFirstFind = 0;
            pFirstFind = NULL;
            _pKey = pKeyFirst;
        }
    
        _pSrc++;
    }
    *_pDest = '\0';

    return pDestFirst;
}

阅读(2109) | 评论(1) | 转发(0) |
0

上一篇:GetPassword

下一篇:StringFind

给主人留下些什么吧!~~

chinaunix网友2009-04-03 20:31:00

有点小 Bug...呵呵 ..