The use, analysis, and simulation implementation of ultra-detailed C language string functions across the entire network

Table of contents

(1), strlen: find the length of a string

1. The role of the function:

2. Function declaration:

3. Function notes:

4. Function usage examples:

5. Simulation implementation of strlen function: (three methods)

(1).Counter method

(2).Pointer - pointer

(3) Recursive method

(2), strcpy: string copy

1. Function declaration:

2. The role of the function:

3. Function notes:

4. Function usage examples:

5. Simulation implementation of strcpy function:

source code:

explain:

(3), strcat: string append

1. Function declaration:

2. The role of the function:

3: Notes on functions:

4: Function usage examples:

5: Simulation implementation of strcat function:

source code:

explain:

(4), strcmp: string comparison

1. Function declaration:

2. The role of the function:

3. Function usage examples:

4. Simulation implementation of strcmp function:

source code:

explain:

(5), strstr: Find substrings in strings

1. Function declaration:

2. The role of the function:

3.Usage examples:

4. Simulation implementation of strstr function:

(6), strtok: string cutting


(1), strlen: find the length of a string

1. The role of the function:

Used to find the length of a string.

2. Function declaration:

size_t strlen ( const char * str );

explain:

(1). The function parameter is a char* pointer str, which is used to receive the first address of the string to be obtained. The function of const is to prevent the pointer str from changing.

(2). The function return value is the length of the string pointed to by str. When '\0' is encountered, statistics ends. The return value type is size_t (unsigned integer).

3. Function notes:

(1). We know that the string uses '\0' as the end mark, and the strlen function returns the number of characters that appear before '\0' in the string.

(2). The string pointed to by parameter str must end with '\0', otherwise unpredictable results will occur.

(3). This function is included in the header file <string.h> .

4. Function usage examples:

As shown below:

 The first three are easy to understand. The array size is 6 bytes. However, when the first three are initialized, '\0' will be automatically added at the end, so one byte of space is needed to store '\0', so the string length is 5. In the fourth initialization method, '\0' will not be automatically added to the end of the string, so strlen will keep counting until it encounters '\0', so it will calculate the random value.

5. Simulation implementation of strlen function: (three methods)

(1).Counter method

The source code is as follows:

//计数器模拟实现strlen
#include<stdio.h>
#include<assert.h>
size_t my_strlen(const char* str)
{
	assert(str);
	size_t count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}

int main()
{
	size_t sz = my_strlen("abcdef");
	printf("该字符串长度为:%zu\n", sz);
	return 0;
}

explain:

①: Since it is a simulation implementation, the parameters and return value types should be the same.

②: According to the statistical principle of strlen function, it stops when it encounters '\0', so we only need to create a variable (counter) and use a while loop. When the value pointed by the pointer str is not equal to '\0', the counter will be +1 , then the pointer +1 determines the next character until the value pointed by str is equal to '\0', and the loop ends. Finally, return the value of the counter.

③: Some friends may not know the assert function. In fact, this function is only used to check whether the pointer str is empty. Friends who are interested can learn about it by themselves, and those who are not interested can ignore it.

(2).Pointer - pointer

The source code is as follows:

//指针-指针实现strlen
#include<assert.h>
#include<stdio.h>
size_t my_strlen(const char* str)
{
	assert(str);
	const char* end = str;
	while (*end != '\0')
	{
		end++;
	}
	return end - str;
}

int main()
{
	size_t sz = my_strlen("abcdef");
	printf("该字符串长度为:%zu\n", sz);
	return 0;
}

explain:

①: First of all, we need to understand one thing, pointer - what does the pointer get?

Answer: Pointer - the pointer gets the number of elements between the two pointers

②: So based on the above, we can create a pointer end of the same type as str, assign str to end, and then point the value pointed by end to '\0' in the string through a while loop, as above, The purpose can be achieved through the ++ operation.

③: Finally, return end - str, which is the number of elements between '\0' and the first address. The schematic diagram is as follows:

(3) Recursive method

The source code is as follows:

//递归实现strlen
size_t my_strlen(const char* str)
{
	if (*str != '\0')
	{
		str++;
		return 1 + my_strlen(str);
	}
	return 0;
}

int main()
{
	size_t sz = my_strlen("abcdef");
	printf("该字符串长度为:%zu\n", sz);
	return 0;
}

explain:

Because recursion is too abstract, we draw a picture to understand it, as shown below:

(2), strcpy: string copy

1. Function declaration:

char * strcpy ( char * destination, const char * source );

explain:

①: The function has two formal parameters :

The first formal parameter is a char* pointer, which is the target space;

The second formal parameter const char* pointer is the source string.

We just need to copy the source string pointed to by the source pointer to the target space.

②: The return value the first address of the target space (convenient for chain access of functions).

③: This function is included in the header file <string.h> .

2. The role of the function:

The reference function declaration is to copy the source string pointed to by the formal parameter source to the target space pointed to by the formal parameter destination, including '\0'.

3. Function notes:

①: The source string must end with '\0'.

②: The '\0' of the source string will also be copied to the target space.

③: The target space must be large enough to ensure that the source string can be stored.

④: The target space must be variable, so it is not modified with const.

4. Function usage examples:

1.Normal use:

The source code is as follows:

#include<string.h>
#include<stdio.h>
int main()
{
	char arr1[] = { 'a','b','c','\0' };
	char arr[10] = " ";
	strcpy(arr, arr1);
	printf("%s\n", arr);
	return 0;
}

operation result:

 2. If the target space is not large enough:

 

3. When the target space is immutable:

 

5. Simulation implementation of strcpy function:

source code:
//模拟实现strcpy
#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* sour)
{
	assert(dest && sour);
	char* tmp = dest;
	while (*dest++ = *sour++)
	{
		;
	}
	return tmp;
}

int main()
{
	char arr[] = "abcdef";
	char dest[10] =" ";
	my_strcpy(dest,arr);
	printf("%s\n", dest);
	return 0;
}
explain:

①: Since it is a simulation implementation, the return type and parameters should be the same.

②: Idea: It is a copy of character by character until it ends with '\0'.

③: Because we need to return the starting address of the target space, we first create a pointer variable tmp to save the starting address of the target space.

④: Then we use a while loop to copy character by character, assign the value pointed by sour to the space pointed by dest, and then determine whether the value pointed by sour is equal to '\0'. If not, then sour and dest Both +1, copy the next character; if it is equal to '\0', copy '\0' and the loop ends, so the ASCII value of '\0' is equal to 0.

⑤: Finally, return the first address tmp of the target space.

(3), strcat: string append

1. Function declaration:

char * strcat ( char * destination, const char * source );

explain:

①: The function has two formal parameters :
the first formal parameter is a char* pointer, which is the string to be appended (target space), so it does not need to be modified by const.

The second formal parameter is a const char* pointer, which is the source string. The source string cannot be changed, so it is modified with const.

This function appends the source string pointed to by source to the end of the string to be appended pointed by destination.

②: The function return value is the first address of the string to be appended.

③: This function is included in the header file <string.h> .

2. The role of the function:

Refer to the function declaration, that is, append the source string pointed to by source to the end of the string to be appended pointed by destination.

3: Notes on functions:

1. The source string source must end with '\0'.

2. The target space must be large enough to accommodate the source string.

3. The target space must be modifiable.

4. Try not to add to yourself.

4: Function usage examples:

The source code is as follows:

#include<string.h>
int main()
{
	char arr[] = "world";
	char dest[20] = "hello ";
	strcat(dest, arr);
	printf("%s\n", dest);
	return 0;
}

operation result:

 

5: Simulation implementation of strcat function:

source code:
//模拟实现strcat
#include<assert.h>
#include<stdio.h>
char* my_strcat(char* destination, const char* source)
{
	assert(destination && source);
	//存目标空间首地址
	char* tmp = destination;
	//找到目标空间'\0'的位置;
	while (*destination)
	{
		destination++;
	}
	//从目标空间'\0'位置开始,将源字符串的字符一个个的拷贝进来,与strcpy类似
	while (*destination++ = *source++)
	{
		;
	}
	return tmp;

}

int main()
{
	char arr[] = "world";
	char dest[20] = "hello ";
	my_strcat(dest, arr);
	printf("%s\n", dest);
	return 0;
}
explain:

①: Because it is a simulation implementation, the return value and parameters should be the same.

②: Idea: First find the '\0' position of the target space, and then starting from this position, copy the content of the source string character by character until the copy ends at '\0' of the source string.

③: Because the return value is the first address of the target space, the first address is first saved in the variable tmp.

④: Then use a while loop to point the pointer destination to the '\0' position of the target space.

⑤: Starting from this position, use the strcpy function to simulate the implementation method and copy the content of the source string character by character until the loop ends after copying to '\0' of the source string.

⑥: Finally, return the first address tmp of the target space.

⑦: This simulation implementation has a shortcoming, that is, it cannot be added to itself.

(4), strcmp: string comparison

1. Function declaration:

int strcmp ( const char * str1, const char * str2 );

explain:

1. This function has two parameters , the parameter types are the same, because only comparison is required and the content of the original string cannot be changed, so it is modified with const.

2. The return type of this function is int , and the return value is applicable according to the following standards.

3. This function is included in the header file <string.h> .

2. The role of the function:

This function is used to compare two strings.

Comparison method :

Take out the characters at the corresponding positions of the two strings and compare the ASCII code values ​​one by one. If the first character is the same, that is, the ASCII code value is the same, compare the ASCII code value of the second character.

Comparison rules :

 

3. Function usage examples:

 

4. Simulation implementation of strcmp function:

source code:
//模拟实现strcmp
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0' && *str2 == '\0')
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;
}

int main()
{
	printf("%d\n", my_strcmp("abc", "azc"));
	return 0;
}
explain:

①: Because it is a simulation implementation, the return value and parameters should be the same.

②: First, a while loop is used to determine whether the two strings are equal, character by character. If they are both equal, 0 will be returned at '\0'; if a certain character is not equal, the loop will exit, and then Return *str1 - *str2, because we compare based on ASCII code value, so if *str1 is greater than *str2, the subtraction between the two returns a number greater than 0. If *str1 is less than *str2, then the two Subtraction returns a number less than 0, which is just in line with the standard.

(5), strstr: Find substrings in strings

1. Function declaration:

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

explain:

1. This function has two parameters , which is to search the main string str1 to see if there is a substring str2.

2.Return value:

If the substring str2 cannot be found in the main string str1, NULL (null pointer) is returned.

If found, the first address of the first occurrence of substring str2 in main string str1 is returned.

2. The role of the function:

That is, find the substring str2 in the main string str1:

Not found, returns NULL.

If found, return the first address of the first occurrence of substring str2 in main string str1.

3.Usage examples:

 

 

4. Simulation implementation of strstr function:


//模拟实现strstr
#include<assert.h>
char* my_strstr(char* str1, char* str2)
{
	assert(str1 && str2);
	char* cp = str1;
	char* s1 = cp;
	char* s2 = str2;
	while (*cp)
	{
		//开始匹配
		s1 = cp;
		s2 = str2;
		while (*s1 && *s2 && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return cp;
		}
		cp++;
	}
	return NULL;
}

int main()
{
	char arr1[] = "abbbcdef";
	char arr2[] = "bbc";
	char* ret = my_strstr(arr1, arr2);
	if (ret != NULL)
		printf("%s\n", ret);
	else
		printf("找不到\n");
	return 0;
}

(6), strtok: string cutting

Because this function is rarely used, only the following description is given;

 

Function usage examples:

 

This is the end of this knowledge, I hope it is helpful to you!

Guess you like

Origin blog.csdn.net/hffh123/article/details/132347710