C/C++ nanny-level explanation of C language library functions---string functions + implementation

 Homepage

There are still unknowns waiting to be explored_Data structure, C language difficulties, small projects-CSDN Blog

Topic column---C language difficulties

C Language Difficulties_There are still unknown blogs waiting to be explored-CSDN Blog

thanks for your support! ! !

Table of contents

I. Introduction

2. Explanation

1. Find the string length function (strlen)

1) Usage: 

2) Points to note:

 2. The length of the string copy function (strcpy) is not limited

1) Usage: 

2) Points to note:

 3. The length of the string append function (strcat) is not limited 

1) Usage: 

2) Points to note:

4. The length of the string comparison function (strcmp) is not limited

1) Usage: 

2) Points to note:

5. The length of the string function (strstr) is not limited

1) Usage: 

2) Points to note:

3. Individual implementation 

 1. Function implementation to find the length of a string (strcpy)

1. Loop

2. Recursion 

3. Pointer arithmetic 

 2. Others can be implemented by yourself


I. Introduction

In daily use, operations on strings are relatively frequent, such as calculating the length of strings, copying strings, etc. Although implementing these functions is not very cumbersome, if we can directly call some library functions, it will be relatively simple in terms of code size. Okay, without further ado, let’s go straight to the explanation.

2. Explanation

Note: The header file of the following library function is string.h

1. Find the string length function (strlen)

1) Usage: 

The parameter is a pointer to the requested string.

2) Points to note:

The termination condition of strlen is: encountering '\0' (excluding '\0');

Its return value type is unsigned integer (unsigned integer - unsigned integer is always >= 0);

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[] = "strstr";
	char str2[] = "str\0str";
	printf("str1:%d\n",strlen(str1));
    printf("str2:%d\n",strlen(str2));
	return 0;
}

 2. The length of the string copy function (strcpy) is not limited

1) Usage: 

destination is the array to be copied to, and source is the array being copied.

2) Points to note:

The char* destination space must be large enough and must be modifiable. Similarly, it ends when encountering '\0'

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[7];//destination
	char str2[] = "str\0str";//source
	strcpy(str1, str2);
	printf("str1:%s\n",str1);
	printf("str2:%s\n",str2);
	return 0;
}

 3. The length of the string append function (strcat) is not limited 

1) Usage: 

 Append the source string to the destination.

2) Points to note:

Note that the space size of the destination must be sufficient.

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[7]="str";
	char str2[] = "str\0str";
	strcat(str1, str2);
	printf("str1:%s\n", str1);
	printf("str2:%s\n", str2);
	return 0;
}

4. The length of the string comparison function (strcmp) is not limited

1) Usage: 

Compare str1 and str2.

2) Points to note:

What strcmp compares is not the length of the string, but its ASCII code value. (Compare the ASCII code values ​​of the first character of str1 and the first character of str2. If it is less than, return <0; if greater, return >0; if equal, compare the next character, and then as above)

#include<stdio.h>
#include<string.h>
int main()
{
    //判断str1和str2的大小关系,并打印大小关系符
	char str1[7] = "str";
	char str2[] = "str\0str";
	int ret = strcmp(str1, str2);
	if (ret > 0)
		printf(">");
	else if (ret < 0)
		printf("<");
	else
		printf("=");
	
	return 0;
}

5. The length of the string function (strstr) is not limited

1) Usage: 

 Search whether str1 contains the string str2, and find the address of the returned string in str1.

2) Points to note:

Before using or processing the return value of strstr(str1,str2), remember not to change the str1 string. If you want to change it, you should wait until the return value is used.

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[7] = "strcmp";
	char str2[] = "str";
	char*ret = strstr(str1, str2);
	if (ret == NULL)
		printf("未找到\n");
	else
	{
		printf("%s", ret);
	}
	return 0;
}

3. Individual implementation 

 1. Function implementation to find the length of a string (strcpy)

 The implementation of this function is relatively simple and can be implemented in many ways.

1. Loop

#include<stdio.h>
int my_strlen2(char* arr)
{
	int i = 0;
	while (*(arr + i) != '\0')//如果遇到'\0'结束,'\0'不算作字符串长度
		i++;
	return i;
}
int main()
{
	char arr[] = "abcdef";
	int ret2 = my_strlen2(arr);
	printf("迭代:%d\n", ret2);
	return 0;
}

2. Recursion 

#include<stdio.h>
int my_strlen1(char* arr)
{
	if (*arr != '\0')
		return 1 + my_strlen1(arr + 1);
	else
		return 0;
}
int main()
{
	char arr[] = "abcdef";
	int ret1 = my_strlen1(arr);
	printf("%d\n", ret1);//递归:
	return 0;
}

3. Pointer arithmetic 

The subtraction of two pointers is equal to the number of pointer type byte multiples between the two addresses in memory. Because both pointers point to character arrays, the arrays are continuous, so they can represent how many elements there are.

#include<stdio.h>
char* my_strlen(char* arr)
{
	char*p=arr;
	while(*p!='\0')
		p++;
	return p;
}
int main()
{
	char arr[] = "abcdef";
	char* ret = my_strlen(arr);
	printf("%d\n", ret-arr);
	return 0;
}

 2. Others can be implemented by yourself

 Tip: In addition to violent solution of strstr, you can also use the KMP algorithm. You can study it yourself.

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/133419392