首页 > 其他 > 详细

Implement strStr()

时间:2014-12-09 15:47:10      阅读:155      评论:0      收藏:0      [点我收藏+]

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.


#include<stdio.h>
#include<string.h>

int strStr(char *haystack, char *needle) {
    int i,j,k;
    if(needle[0]=='\0') return 0;

    for(i=0;haystack[i]!='\0';i++){
        if(haystack[i]==needle[0]) {
            for(j=0,k=i;needle[j]!='\0';j++,k++){
                if(haystack[k]!=needle[j]) {
                    if(haystack[k]!='\0') break;
                    else return -1;
                }
            }
            if(needle[j]=='\0')
                return i;
        }
    }
    return -1;
}

void main(){
    printf("%d\n",strStr("abebef","bef"));
}




Implement strStr()

原文:http://blog.csdn.net/uj_mosquito/article/details/41822911

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!