Bingbing study notes: simulation implementation of strstr library function

Function introduction:

The strstr library function is a library function for processing strings, and the header file is <string.h>.

The function of this function is to find in a string whether another string is a subset of the string.

That is, look for the starting position of the first occurrence of src in dest, and return a pointer to that position. If src does not appear completely in dest, a null pointer is returned. If src is an empty string, dest is returned.

A mock implementation of the function:

Analysis code logic:

First, we receive two pointers s1 and s2 to the beginning of the string

Then, it is judged whether the contents pointed to by s1 and s2 are equal. If they are equal, then s1 and s2 move backward and continue to compare the next pair of characters. After the move, if the two strings are found to be unequal, s1 should go back and s2 should also go back. We know that s2 only needs to go back to the beginning of the source string, so where does s1 go back to?

In the above example, we want to find the string "abcd" in the string "abcbbabcqfgabcdeff". When s1 points to character b and s2 points to character d, it is found that they are not equal, s2 returns to the starting position of the character, and s1 should return to the next pointed character at the starting equal position, that is, where the first b appears.

So we need to use the pointer cur to record the address at the beginning equal position in order to fall back here.

 

At this point, we return to the position pointed to by the b character for the next comparison, s1 points to the character b, s2 points to the character a, not equal, s2 continues to fall back to the starting position of the source character. Is s1 the next address recorded back to cur? Of course not, if so, s1 can't move backwards and will stay here forever.

Since the movement of s1 is not easy to achieve, then we let cur move backward to find the same starting position. After finding it, cur points to the same starting position, tells s1 the position, and then does not move by itself, and s1 starts to compare characters from here until no more The same falls back to the next pointer starting the same, then cur moves again. In this way, cur records the position at the beginning of the same character every time, and s1 is used to compare the characters backward from the starting position.

At this time, you will find that a loop is impossible, cur moving backward requires a loop to find the same position where the characters start, and s1 needs a loop to find whether the string continues to be the same after the beginning is the same.

When s1 wants to jump out of the loop, there are only three cases, one is that the characters are found to be unequal, the loop stops, cur moves backward, and starts to find the next same starting point; the other is to find the '\0' of the source string, The end of the source string search means that it is a subset. At this time, the loop stops and returns a pointer to the same position as the beginning, which is the position pointed to by cur at this time. The third is to find the '\0' of the target string, which is also the end of the search.

What if the string pointed to by src is not a subset of the string pointed to by dest? That is, cur finds '\0', at this point the search ends and a null pointer is returned.

Code:

char* my_strstr(const char* dest, const char* src)
{
	assert(dest && src);
	const char* s1 = dest;
	const char* s2 = src;
	const char* cur = dest;//记录开始相等的起始位置
	while (*cur)
	{
		s1 = cur;
		s2 = src;
		while (*s1&&*s2&&(*s1==*s2))//从开始相同位置进行查找后方的字符是否相等
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')//判断是否找到
		{
			return (char*)cur;
		}
		cur++;
	}
	
	return NULL;
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324113398&siteId=291194637