Implement strStr ().
Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.
- The main idea is to find a short string in the haystack of a large string needle the first occurrence of the position
intSTRSTR (Char* Haystack,Char*needle) { inti =0, j =0; intHaysize =strlen (haystack); intNeedlesize =strlen (needle); if(Needlesize = =0) return 0; while(I <= haysize-needlesize) { if(* (haystack + i) = = *needle)//find the first same point { while(* (needle + j))//on the needle cycle { if(* (haystack + i + j) = = * (needle + j))//if the following are the same, continue to judgeJ + +; Else //the difference jumps out{J=0; Break; } } if(!* (needle + j))//If the judgment is final, the first time that the description appears, return I returni; } I++; } return-1;}
- In this topic, the default if needle is empty, return 0
Implement StrStr ()