Advanced C language (string function and simulation implementation strncpy&strncat&strncmp&strstr&strtok&strerror) 2

content

foreword

First, what is the string function

2. What are the string functions and how to simulate and implement them?

1. Length-limited string functions

1. String copy function

(1) Notes on strncpy function

(2) Simulation implementation of strncpy function

2. String append function

(1) Notes on strncat function

(2) Simulation implementation of strncat function

3. Character size comparison function

(1) Notes on strncmp function

4. String lookup function

(1) Notes on strstr function

(2) Simulation implementation of strstr function

5. Cut string function

(1) Notes on strtok function

(2) Simulation implementation of strtok function

6. Convert the error code to an error message function

(1) Notes on strerror function

(2) Simulation implementation of strerror function


foreword

The processing of characters and strings in the C language is very frequent, but the C language itself does not have a string type, and the strings are usually placed in constant strings or character arrays. String literals are available for string functions that do not modify them.

First, what is the string function

String processing function, also known as string processing function, refers to the function used to process strings in programming languages.

2. What are the string functions and how to simulate and implement them?

1. Length-limited string functions

1. String copy function

(1) Notes on strncpy function

Copies num characters from the source string to the destination space.

If the length of the source string is less than num, after copying the original string,

Append 0 to the end of the target, up to num.

(2) Simulation implementation of strncpy function

#include<stdio.h>
#include<assert.h>
char* my_strncpy(char* str1, const char* str2, int nums)
{
	assert(str1 && str2);
	char* ret = str1;
	while (nums)//把str2拷贝到str1,剩余\0没拷贝
	{
		*str1++ = *str2++;
		nums--;
	}
	if (nums > 0)
	{
		while (--nums)//拷贝/0
		{
			*str1++ = '\0';
		}
	}
	return ret;
}
int main()
{
	char arr1[20] = "############";
	char arr2[] = "bit education";
	my_strncpy(arr1, arr2, 4);
	printf("%s\n", arr1);

	return 0;
}

2. String append function

(1) Notes on strncat function

Append num characters from the source string to the destination space

If the length of the source string is less than num, \0 will also be appended after appending, but if there are not enough characters, \0 will not be filled

(2) Simulation implementation of strncat function

 

#include<stdio.h>
#include<assert.h>
char* my_strncat(char* str1, const char* str2, int num)
{
	char* ret = str1;
	assert(str1 && str2);
	while (*str1)
	{
		str1++;
	}
	while (num--)
	{
		if ((*str1++ = *str2++) == 0)
		{
			return ret;
		}
		
	}
	*str1 = '\0';
	return ret;
}
int main()
{
	char arr1[20] = "hello";
	char arr2[] = "world";
	my_strncat(arr1, arr2, 4);
	printf("%s\n", arr1);

	return 0;
}

3. Character size comparison function

(1) Notes on strncmp function

Compare until another character is different or a string ends or all num characters are compared

4. String lookup function

(1) Notes on strstr function

Returns the position of the first occurrence of str2 in str1 in a pointer

(2) Simulation implementation of strstr function

#include<stdio.h>
#include<assert.h>

char* my_strstr(const char* str1, const char* str2 )
{
	assert(str1 && str2);
	const char* s1 = NULL;
	const char* s2 = NULL;
	char* ret = str1;
	while (*ret)
	{
		s1 = ret;
		s2 = str2;

		while (*s1 && *s2 && (*s1 == *s2))
		{
			
				s1++;
			    s2++;
				if (*s2 == '\0')
				{
					return ret;
				}
		}
		ret++;
	}
	return NULL;
}
int main()
{
	char arr1[20] = "abbbcdef";
	char arr2[] = "bbc";
	char* ret = my_strstr(arr1, arr2);
	if (ret != NULL)
	{
		printf("找到了:>%s\n", ret);
	}
	else
	{
		printf("查找的字符串找不到\n");
	}

	return 0;
}

5. Cut string function

(1) Notes on strtok function

The first parameter specifies a string containing zero or more tokens separated by one or more delimiters in the sep string. The strtok function finds the next token in str, ends it with \0, and returns a pointer to this token. The first parameter of the strtok function is not NULL, the function will find the first token in str, and the strtok function will save its position in the string. The first parameter of the strtok function is NULL, and the function will start at the saved position in the same string and look for the next token. If there are no more tokens in the string, a NULL pointer is returned.

(2) Simulation implementation of strtok function

#include<stdio.h>



int main()
{
	char arr1[20] = "[email protected]";
	char arr2[20] = { 0 };
	char *p = "@.";
	strcpy(arr2, arr1);
	/*printf("%s\n", strtok(arr2, p));
	printf("%s\n", strtok(NULL, p));
	printf("%s\n", strtok(NULL, p));*/
	char* ret = NULL;
	for (ret = strtok(arr2, p); ret != NULL; strtok(NULL, p))
	{
		printf("%s\n", ret);
	}
	//第一次穿非空指针,第二次传空指针
	//strtok函数的第一个参数不为NULL,函数将找到arr2中的第一个标记,strtok函数将保存他在字符串中的位置。
	//strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
	return 0;
}


6. Convert the error code to an error message function

(1) Notes on strerror function

Returns the error code and the corresponding error message.

(2) Simulation implementation of strerror function

#include<stdio.h>
#include<string.h>
#include<errno.h>

     //int errno;//全局错误码,存放错误信息的变量
int main()
{
	FILE* pf = fopen("test.txt", "r");//打开文件失败返回NULL
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
	}
	return 0;
}

Summarize

This article only briefly introduces the use of commonly used string functions. C language provides us with a large number of functions and methods that can make us process data conveniently and quickly. We need to have a deeper understanding and learn how to see and use unknown libraries. function, in addition, if there is any problem with the article, welcome to ask questions. I will learn and correct with an open mind. The most important thing is to make progress together, grow together, and learn programming well.

Guess you like

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