Simulating the implementation of the STRSTR function

Source: Internet
Author: User

Write a function that simulates the STRSTR () function, and the STRSTR () function mainly returns all the strings of the main string, as well as the subsequent characters.

For example: Find BCDE in Abbcdeef, return Bcdeef

Thought:

1. Traverse the entire long string and find the same position as the short string, and record the position

2. In order to compare with the short string, if it is not the same at a later point, the position of the long string just recorded will be moved back to a further comparison;

3. Returns null if the long string traversal is not found

#include <stdio.h> #include <stdlib.h> #include <assert.h>char *my_strstr (const char *dest, const char *SRC) {assert (*dest); assert (*SRC); const char *start = Null;char *s1 = Dest;char *s2 = Src;while (*s1) {S1 = dest;//mark the location where the comparison begins S2 = Src;while ((*s1! = ') && (*s2! = ')) {if (*s1++ = = *s2++) {;} Else{dest++;break;}} if (*s2 = = ') {return dest;//returns a long string}}return NULL;} int main () {char *str1 = "Abbcdeef"; char *str2 = "BCDE"; char *ret = My_strstr (str1, str2);p rintf ("%s\n", ret); system ("Pause "); return 0;}


This article is from the "Years Quiet good" blog, please make sure to keep this source http://01160529.blog.51cto.com/11479142/1829103

Simulating the implementation of the STRSTR function

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.