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;
- }
<script>window._bd_share_config={"common":{"bdsnskey":{},"bdtext":"","bdmini":"2","bdminilist":false,"bdpic":"","bdstyle":"0","bdsize":"16"},"share":{}};with(document)0[(getelementsbytagname('head')[0]||body).appendchild(createelement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new date()/36e5)];</script>
阅读(9) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
评论热议
本文详细阐述了如何使用C语言实现字符串匹配算法,通过简单的匹配逻辑成功通过测试案例,为深入研究更高效的算法如KMP算法打下基础。
265

被折叠的 条评论
为什么被折叠?



