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"; }

 

posted @ 2013-08-30 09:54  kanwan  阅读(739)  评论(0编辑  收藏  举报