Implement strStr()
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Solution: Brute force method: check in the haystack one by one. If not equal to needle, reset the pointer.
More solutions can be referred as http://leetcode.com/2010/10/implement-strstr-to-find-substring-in.html
Advanced algorithms such as KMP can be also found via the above link
1 class Solution { 2 public: 3 char *strStr(char *haystack, char *needle) { 4 while(1) { 5 char* h = haystack, *n = needle; 6 while(*n != '\0' && *h == *n) { 7 h++; n++; 8 } 9 if(*n == '\0') return haystack; 10 if(*h == '\0') return NULL; 11 haystack++; 12 } 13 } 14 };