// test24.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; int find_substr(char * src,char * des,int pos){ int i=pos,j=0; while(src[i]&&des[j]){ if(src[i]==des[j]){ i++; j++; } else{ i=i-j+1; j=0; } } if(j>strlen(des)-1) //0 起步所以判断的是长度-1; return i-strlen(des); else return -1; } int main(int argc, char* argv[]) { char * src="nihaowoshizhangjie"; char * des="shi"; cout<<find_substr(src,des,0)<<endl; return 0; }