Simulation to realize strstr function _c

Source: Internet
Author: User
Tags assert first string
Simulating the implementation of STRSTR functions

First, you need to understand what the STRSTR function does: The STRSTR function is to determine if the string you need to find is in the original string, and if so, the position of the output string, no, output empty.

For example: Give an array of two characters: str1[20]= "ABCDEFG", str2[10]= "BCD"; you can get "BCDEFG" by using the STRSTR function, and the output is the position of the string you want to find.

Then, let's talk about the way to implement this function.

First of all, according to the STRSTR function to realize that the function needs to pass the parameter is two strings, and these two strings are immutable, so that we can use the const to decorate, and the function returns a string, then the return value type can also be determined, so that the We can write the definition of such a function: char* my_strstr (const char* dest,const char* src) {}. Where dest points to the target string, which is the string you need to compare, src points to the source string, which is the string to be compared. A null pointer can be returned if it is not found.

The implementation of the idea is also relatively simple, you can create two pointers, through traversal of the way to access the string, and compare the two pointers.

////

For example: Give the last two strings:

Str1:abcdefg

Str2:bcd

Two pointers were established: Char *start=str1; char *substart=str2;

At this point, start points to character a, and Substart points to character B, and compares the start and substart two pointers to see if they are equal, that is, *start=*substart, and if two are equal, two pointers are moved back one bit, then the reference is judged If two are not equal, then start moves backwards one bit, substart does not move, and two are compared again. Repeat this process until one or two of the two strings encounter ' yes ' and the traversal ends. Then you can do it in a circular way, and the judgment condition of the loop is (start!= ' && substart!= ' ") && *start==*substart.

////

If you come here and you feel that this function succeeds, you fall into the pit. Let me give you another example:

Str1:abbbcdefg

Str2:bbcd

Here you continue to do it the way you do, and you will get a different result.

The pointer determines that when start points to the first B in the str1, the Substart points to the first B in str2, two equals, and then traverses back, and when start points to the third B, Substart points to C, two unequal, Then Substart will return and point to the first B in str2.

Note that at this point the start is pointing to the third b,start in the str1 and not moving, and then comparing it in the same way, you can no longer find the same as str2 from the third B of str1, then the function returns an empty But obviously the first string contains the string you want to find. In this way, we need some way to solve this problem.

The solution is also simpler: When two string comparisons have unequal results, we know that Substart is the pointer to the second string that is returned to the starting position of the STR2 string, and the start pointer does not change anything. The comparison can only be continued from this position backward comparison, then there must be some characters are no way to compare. So, when the two characters are not equal, the start pointer also needs to return forward, but it is not going back to the starting position, but to the next position in the starting position, so that you can avoid duplicate and useless comparisons. So we need to create a pointer CP to hold the starting position and let start return to the next position in the start position.

////

The specific code implementation method is as follows:

</pre><pre name= "code" class= "CPP" > #define _CRT_SECURE_NO_WARINGS 1

#include <stdio.h>
# include<stdlib.h>
#include <assert.h>
char* my_strstr (const char* dest, const char* src)
{
	char* start = (char*) dest;//here need to force type conversion to char*
	char* substart = (char*) src;
	char* CP = (char*) DEST;//CP is the
	assert (dest!= NULL) used to hold the first address;
	ASSERT (src!= NULL);
	while (*CP)
	{
		start = CP;
		while (*start!= ' && *substart!= ') && *start = = *substart)
		{
			start++;
			substart++;
		}
		if (*substart = = ' ")
		{return
			cp;
		}
		Substart = (char*) src;
		cp++;//cp++ can get the next position from the original starting position
	} return
	NULL;
}

The following is a test function:
int main ()
{
	char a[20] = "Abbbcdef";
	Char b[10] = "BBCDE";
	printf ("%s\n", My_strstr (A, b));
	System ("pause");
	return 0;
}


The results of the test are as follows:



Please point out any deficiencies in order to correct them.



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.