[C Language] implement strstr Functions

Source: Internet
Author: User

[C Language] implement strstr Functions

# Include <stdio. h> # include <assert. h> char * my_strstr (const char * str1, const char * str2) {assert (str1); assert (str2); char * p = str1; // record the initial position of str1 char * s1 = p; // record the initial position of str1 char * s2 = str2; // record the initial position of str2 while (* p) {s = p; // Add 1 to the start position of str1 from the start position of the previous time, the following p ++ calculates the while (* p & * q & * p = * q) // if it does not match, str2 starts from the first element {p ++; q ++;} if (* q = '\ 0') {return p;} p ++; // mismatch. str1 is backward added by 1} return NULL from the last starting position; // The result is not found after the overall traversal.} int main () {char * str1 = "abbbcdef"; char * str2 = "bbcd"; char * ret = my_strstr (str1, str2); printf ("% s \ n", ret ); return 0 ;}

 

Related Article

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.