Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
class Solution {
public:
int strStr(char *haystack, char *needle) {
if(!haystack||!needle) return -1;
if(*needle=='\0') return 0;
char *p1=haystack;char *p2=needle;int k=0;char *temp=haystack;
while(*p1!='\0'){
if(*p1==*p2){
temp=p1;
temp++;p2++;
while(*p2!='\0'){
if(*temp!=*p2) break;
temp++;p2++;
}
if(*p2=='\0') return k;
}
if(*temp=='\0') break;
p2=needle;
p1++;
k++;
}
return -1;
}
};