字符串查询-Sunday算法源码实现

在指定字符串中查找子串,推荐试用Sunday算法,具体SunDay算法的介绍,推荐阅读下面网址:https://blog.csdn.net/q547550831/article/details/51860017

作者根据Sunday算法,整理源码如下,其中,先实现SundayMemMem函数,这个函数实现的功能是:在指定长度的内存空间中,查找指定长度的字节序列,SundayStrStr函数进一步简单封装SundayMemMem,实现子串查询。源码已经由作者进行了充分的测试,可放心使用。谢谢!

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char* SundayMemMem(char* sText, int nTextLength, char* sKeyWord, int nKeyLength)
{    
    if(nKeyLength == 0)
    {
        return NULL;
    }

    if(nTextLength == 0)
    {
        return NULL;
    }

    if(nKeyLength > nTextLength)
    {
        return NULL;
    }

    int nPosArray[256];    
    memset(nPosArray, 0, 256*sizeof(int));    

    int i;
    i = 0;

    char* pShift;
    pShift = sKeyWord + nKeyLength - 1;

    char* pTail;
    pTail = sKeyWord + nKeyLength;

    while(pShift >= sKeyWord)
    {
        unsigned char ch;
        ch = *pShift;

        if(nPosArray[ch] == 0/*nKeyLength+1*/)
        {
            nPosArray[ch] = pTail - pShift;
            i++;
            if(i == 256)
            {
                break;
            }
        }
        pShift--;
    }
    //        
    pShift = sText;

    char* pOffset;
    pOffset = sKeyWord;

    char* pBase;
    pBase = pShift;

    int nMatched;
    nMatched = 0;

    char* pKeyTail = sKeyWord + nKeyLength;
    char* pTextTail = sText + nTextLength;

    while((pOffset < pKeyTail) && (pShift < pTextTail))
    {
        if (*pShift != *pOffset)
        {
            pShift = pBase + nKeyLength;
            unsigned char ch;
            ch = *pShift;
            int nPos;
            nPos = nPosArray[ch];
            if(nPos == 0)
            {                
                pBase += nKeyLength+1;
            }
            else
            {                
                pBase += nPos;
            }
            pShift = pBase;
            pOffset = sKeyWord;
            nMatched = 0;
            continue;
        }
        pShift++;
        pOffset++;
        nMatched++;
    }

    if(nMatched == nKeyLength)
    {        
        return pBase;
    }
    else
    {        
        return NULL;
    }
}

char* SundayStrStr(char* sText, char* sKeyWord)
{
    return SundayMemMem(sText, strlen(sText), sKeyWord, strlen(sKeyWord));    
}

// 测试子串查询

int main(int argc, char* argv[])
{
    char* sText = "1234567890abcedf";

    char* pFind;
    pFind = SundayStrStr(sText, "0abc");

    if(pFind  != NULL)
    {

        printf("pFind = %s\r\n", pFind );
    }
    else
    {
        printf("No find KeyWord\r\n" );
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值