首页 > 代码库 > LeetCode:Implement strStr()

LeetCode:Implement strStr()

题目描述:

Implement strStr().

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

代码:

int Solution::strStr(char *haystack, char *needle)
{
    int haystack_length = strlen(haystack);
    int needle_length = strlen(needle);
    for(int i = 0;i < haystack_length-needle_length+1;i++)
    {
        int index = i;
        int j;
        for(j = 0;j < needle_length;j++,index++)
        {
            if(haystack[index] == needle[j])
                continue;
            else
                break;
        }
        if(j == needle_length)
            return i;
    }
    return -1;
}


LeetCode:Implement strStr()