Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Solution:Code:
<span style="font-size:14px;">class Solution {
public:
char *strStr(char *haystack, char *needle) {
const int lengthH = strlen(haystack);
const int lengthN = strlen(needle);
for (int i = 0; i <= lengthH-lengthN; ++i) {
bool match = true;
for (int j = 0; j < lengthN; ++j)
if (haystack[i+j] != needle[j]) {
match = false;
break;
}
if (match) return &haystack[i];
}
return NULL;
}
};</span>