Implement StrStr ()

Source: Internet
Author: User

Implement strStr ().

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

    • The main idea is to find a short string in the haystack of a large string needle the first occurrence of the position
intSTRSTR (Char* Haystack,Char*needle) {    inti =0, j =0; intHaysize =strlen (haystack); intNeedlesize =strlen (needle); if(Needlesize = =0)        return 0;  while(I <= haysize-needlesize) {        if(* (haystack + i) = = *needle)//find the first same point        {             while(* (needle + j))//on the needle cycle            {                if(* (haystack + i + j) = = * (needle + j))//if the following are the same, continue to judgeJ + +; Else    //the difference jumps out{J=0;  Break; }            }            if(!* (needle + j))//If the judgment is final, the first time that the description appears, return I                returni; } I++; }    return-1;}
    • In this topic, the default if needle is empty, return 0

Implement StrStr ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.