Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
我试着用了最简单的匹配,居然过了,以后再用KMP之类的好好研究一下。
- char *strStr(char *haystack, char *needle) {
- if(haystack==NULL || needle==NULL)
- return NULL;
- char * head=haystack;
-
- while(head!='\0'){
- char *p0=head;
- char * p=needle;
- while(*p!='\0' && *p0!='\0'){
- if(*p!=*p0){
- break;
- }
- p++;
- p0++;
- }
- if(*p=='\0'){
- return head;
- }
- if(*p0=='\0'){
- return NULL;
- }
- head++;
- }
- return NULL;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议