#include <QCoreApplication>
#include<stdio.h>
#include<string.h>
int stringMatch(char* T,char* P,int nLenT, int nLenP)
{
int i = 0, j = 0;
while(i<(nLenT- nLenP+1)) //i最大位置仅能取到nLenT- nLenP
{
if(T[i+j] == P[j]) //当子字符串开头匹配就一直判断下去
{
if(j == nLenP -1)//当整个字符都匹配返回i的位置
return i;
j++; //未匹配完成,则匹配下一位
}
else //如果不匹配则i位置+1,j从头开始
{
i++;
j=0;
}
}
return -1;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
{
char T[]="goodbye BeiJing,hello Wuhan!" ;
char P[]="hello";
int nLenT , nLenP;
int nRel;
nLenT = strlen(T);
nLenP = strlen(P);
nRel = stringMatch(T,P,nLenT,nLenP);
printf("%d\n",nRel);
nRel = stringMatch(T,P,nLenT,nLenP);
}
{
char *s=(char *)"goodbye BeiJing,hello Wuhan!" ;
char *l=(char *)"hello";
char *p;
//clrscr();
p=strstr(s,l);
if(p)
{
printf("%d,%s",p-s,p);
}
else
{
printf("Not Found!");
}
//getchar();
}
return a.exec();
}