C++查找子串

void sub_string(char str[],char sub[])
{

 int num=0,j=0;
 int len = strlen(str);
 int sublen =strlen(sub);


 if (sublen>len)
 {//子串比总串长
  cout<<"sublen more than len ,error";
exit(1); }
for(int i = 0;i<=len-sublen+1;i++) { if (str[i] == sub[0]) { for (j=0;j<sublen&&(i+j)<len&&(str[i+j]==sub[j]);j++) ; if(j==sublen) {num = i;j=0;} } } if(num!=0) cout<<num; else cout<<"not found"; }
A man can fail many times,but he isn't a failure until he begins to blame somebody else.
原文地址:https://www.cnblogs.com/kanwan/p/3290813.html