码迷,mamicode.com
首页 > 其他好文 > 详细

Implement strStr()

时间:2014-12-09 15:48:41      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   color   for   on   art   ef   as   

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()

标签:style   io   ar   color   for   on   art   ef   as   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!