char *substring,*string; int same; substring="???gram"; string="this program return index of substring"; same=pattern_index(substring, string); if(same) cout<<"子串起始位置: "<<same<<end1; else cout<<"匹配不成功" <<end1;
【正确答案】 int pattern_index(char substr[],char str[]) { int i,j,k; for(i=0;str[i];i++) for(j=i,k=0;(str[j]==substr[k])||(substr[k]=='?');j++,k++) if(!substr[k+1]) return(i); return(0); }