题目
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
即寻找子串
代码:
class Solution {
public:
char *strStr(char *haystack, char *needle) {
if(needle==NULL||haystack==NULL)
return NULL;
int lenhay=strlen(haystack);
int lenneed=strlen(needle);
if(lenhay<lenneed)
return NULL;
char *p,*q;
for(int i=0;i<=lenhay-lenneed;i++)
{
p=haystack+i;
q=needle;
while(*p!='\0')
{
if(*p!=*q)
break;
p++;
q++;
}
if(*q=='\0')
return haystack+i;
}
return NULL;
}
};