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 };

相关文章:

  • 2021-08-21
  • 2022-02-10
  • 2022-01-23
  • 2021-10-26
  • 2022-12-23
猜你喜欢
  • 2021-06-10
  • 2021-12-16
  • 2021-12-05
  • 2021-07-18
  • 2021-06-28
  • 2022-01-15
相关资源
相似解决方案