//实现了 count,但是针对最后的 关键字不能计算在内
#include<stdio.h>
#include<string.h>
int ISFindTheCh(char * const target,char ch);
int IsMatch(char *const source,char * target);
int main()
{
char string[] = "hellohello world!,worryhellow";
char str[] ="wor";
static int numWord = 0;
static int count = 0;
int Source_Lenth =(int) strlen(string);
int Target_Lenth = (int) strlen(str);
//while( count + Target_Lenth < Source_Lenth)
while( count < Source_Lenth)
{
char *strterm;
if(count == 0)
strterm = string;
else
strterm = string +count+numWord*Target_Lenth;
int offset = IsMatch(strterm,str);
if (offset < 0)
{
printf("No Find '%s' again. \n",str);
printf("the find num of the '%s' is %d.\n",str,numWord);
return 0;
}
numWord +=1;
count += offset;
printf("Find '%s' in offset: %d \n",str,count);
}
return 1 ;
}
int ISFindTheCh(char * const target,char ch)
{
char * p = strrchr(target,ch);//从后往前匹配的第一个字符
if(p)
return (int)(p - target);
else
return -1;
}
int IsMatch(char *const source,char * target)
{
int Source_Lenth =(int) strlen(source);
int Target_Lenth = (int)strlen(target);
char *Sp = source;//源文件指针
char *St = target;//目标文件指针
char *SumP = source + Target_Lenth;
// if ((Source_Lenth - Target_Lenth) <0)
// return -1;
int offset = -1;
int count = 0;
while(Source_Lenth > Target_Lenth)
{
if(*Sp == *St)
{
Sp++;
St++;
Source_Lenth--;
count++;
if(count == Target_Lenth)
{
// printf("---Find '%s' in offset: %ld \n",target,Sp-source-count);
return (int)(Sp-source-count);
}
}
else
{
offset = ISFindTheCh(target,SumP[0]);
if(offset == -1)
{
Sp = SumP +1 ;
SumP = Sp + Target_Lenth;
}
else
{
Sp = SumP - offset;
Source_Lenth += offset;
}
St = target;
count = 0;
Source_Lenth -= Target_Lenth ;
}
}
// printf("No Find '%s' again. \n",target);
return -1;
}
//不能计数count、
#include<stdio.h>
#include<string.h>
int ISFindTheCh(char * const target,char ch);
int IsMatch(char *const source,char * target);
int mian()
{
char string[] = "hello world!";
char str[] ="str";
int is;
if((is = IsMatch(string,str)))
printf("Find '%s' in offset: %d \n",str,is);
return 1 ;
}
int ISFindTheCh(char * const target,char ch)
{
char * p = strrchr(target,ch);//从后往前匹配的第一个字符
if(p)
return p - target;
else
return -1;
}
int IsMatch(char *const source,char * target)
{
int Source_Lenth = strlen(source);
int Target_Lenth = strlen(target);
char *Sp = source;//源文件指针
char *St = target;//目标文件指针
char *SumP = source + Target_Lenth;
int offset = -1;
int count = 0;
while(Source_Lenth > 0)
{
if(*Sp == *St)
{
Sp++;
St++;
Source_Lenth--;
count++;
if(count == Target_Lenth)
return Sp-source-count;
}
else
{
offset = ISFindTheCh(source,SumP[0]);
if(offset == -1)
{
Sp = SumP +1 ;
SumP = Sp + Target_Lenth;
}
else
{
Sp = SumP - offset;
Source_Lenth += offset;
}
St = target;
count = 0;
Source_Lenth -= Target_Lenth ;
}
}
return -1;
}