Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
1 class Solution { 2 public: 3 char *strStr(char *haystack, char *needle) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int hayLen = strlen(haystack); 7 int needLen = strlen(needle); 8 9 for(int i = 0; i <= hayLen - needLen; i++) 10 { 11 char *p = haystack + i; 12 char *q = needle; 13 while(*q != '\0') 14 { 15 if (*p != *q) 16 break; 17 else 18 { 19 p++; 20 q++; 21 } 22 } 23 24 if (*q == '\0') 25 return haystack + i; 26 } 27 28 return NULL; 29 } 30 };