#include <string.h>
#include <iostream>
using namespace std;
int BFStringCmpare(const char *strLong,const char *strShort,int iLong,int iShort)
{
if(iLong<iShort)
return -1;
int ipos=0;
int i,j;
for(i=0;i<iLong;i++)
{
//如果当前主串剩余长度小于子串,无需在比较
if((iLong-i)<iShort)
{
break;
}
ipos=i;
j=0;
//比较是否与子串的第一个字符相同,相同则继续比较
//否则主串向后移
if(*(strLong+i)==*(strShort+j))
{
i++;
j++;
while((j<iShort)&&(i<iLong))
{
if(*(strLong+i)==*(strShort+j))
{
i++;
j++;
}
else
break;
}
//判断是否比较成功
if(j==iShort)
break;
}
}
if(j==iShort)
return ipos;
else
return -1;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *strLong="love zy_dreamer ";
char *strShort="dreame
字符串——Brute Force实现
最新推荐文章于 2024-05-05 22:11:48 发布