代码:
#include <iostream> using namespace std; int main() { char *s="helolophp"; char *s1="lop"; char *c=s1; int len=0; while(c[len]!='\0') { len++; } int i=0,j=0; for(;s[i]!='\0';i++) { if(s1[j]==s[i]) { j++; if(j==len) { cout<<"匹配成功的起始位置是:"<<(i-j+2)<<endl; break; } continue; } else { i=i-j+1; j=0; } } if(s[i]=='\0') cout<<"匹配不成功。"<<endl; return 0; }
运行结果: