題目如題目所示。
代碼如下:
www.2cto.com
int findSubStringNUM(const char* targetString,const char* subString){
assert(NULL!=targetString||NULL!=subString);
int i=0;
int j=0;
int num=0;
while(targetString[i]!='\0'){
if(subString[j]!='\0'){
if(targetString[i]==subString[j]){i++;j++;}
else {i=i-j+2;j=1;}
}
else {num++;i=i-j+2;j=1;}
}
return num;
}
int main(){
int num=findSubStringNUM1("hellohellohello","he");
cout<<num<<endl;
return 0;
}
摘自 日新為道的專欄