Functions related to strings

1. A function to find the length of a string

strlen

2. String function with unlimited length

strcpy

strcmp

strcat

3. String functions with restricted length

strncpy

strncat

strncmp

Four, string search

strstr

strtok

Five, error information report

strerror

 

 

 

 

1. A function to find the length of a string

strlen

note:

1. The strlen function returns the number of characters appearing before'\0' in the string (not including'\0')

2. The parameter type of the function is a pointer of type char, and the return value is size_t (unsigned integer)

3. Because the return value is size_t, the return value of the function cannot be compared or calculated

 

(Dig a hole) The analog implementation of strlen (1. Counter method 2. Recursive method 3. Pointer minus pointer method)

 

 

2. String function with unlimited length

 

1. String copy function

strcpy

Note: 1. Ensure that the target space is large enough

           2. The target space must be variable, so const cannot be added before the first parameter

Parameter type: 1.char *strDestination

                  2.char *strSource

Return type: char*

 

Simulated implementation of strcpy

Normal version (header file):

//strcpy的模拟实现
void my_strcpy(char *dest, char *src)
{
	while (*src != '\0')
	{
		*dest = *src;
		src++;
		dest++;
	}
	*dest = *src;
}
int main()
{
	char arr1[20] = "#########";
	char arr2[20] = "hello world";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

Premium version:

//strcpy的模拟实现
#include<string.h>
#include<assert.h>
char* my_strcpy(char *dest,const char *src)
{
	assert(dest && src);//断言,提高指针的安全性
	char *ret = dest;
	while (*dest++ = *src++)//字符串的拷贝,先拷贝,后++,当把'\0'也拷贝过去后,括号里表达式结果为0,为假,跳出循环
	{
		;
	}
	return ret;//返回目标空间的起始地址
}
int main()
{
	char arr1[20] = "#########";
	char arr2[20] = "hello world";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

 

2. String append function

strcat

Note: 1. The target space to be added must be large enough

           2. The space to be added must be modifiable

           3. The target string and source string must have'\0'

           4. Can't add to yourself

Parameters: 1.char *strDestination

           2.char *strSource

Return type: char*

Simulation implementation

//strcat的模拟实现
#include<assert.h>
char* my_strcat(char *dest, const char *src)
{
	assert(dest && src);
	char *ret=dest;
	//1.找到目标字符串的'\0'
	while (*dest)
	{
		dest++;
	}
	//2.追加
	while (*dest++ = *src++);
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "hello ";
	char arr2[] = "world";
	
	printf("%s\n", my_strcat(arr1, arr2));
	return 0;
}

 

3. String comparison function

strcmp

note:

1. The comparison is the ASCII code value of the corresponding character, not the length of the string

2. The former>the latter, returns a number>0; the former=the latter, returns 0; the former<the latter, returns a number<0

 

Parameters: 1.char *str1

           2.char *str2

Return type: int

Simulation implementation

//strcmp的模拟实现
#include<assert.h>
int my_strcmp(const char *s1, const char *s2)
{
	assert(s1 && s2);
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;
		s1++;
		s2++;
	}
	return *s1 - *s2;
}
int main()
{
	char arr1[10] = "abc";
	char arr2[10] = "abcde";
	int ret = my_strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}

 

 

3. String function with restricted length (compared with restricted function, there is one more parameter size_t count, which is used to record the number of characters to be operated)

Similar to the function with unlimited length, it is simpler and not too much explanation

 

Four, string search function

1.strstr (find substring)

Situations that can be found:

//strstr查找子串函数
#include<string.h>
int main()
{
	char arr1[20] = "abcdef";
	char arr2[20] = "bcde";
	char *ret = strstr(arr1, arr2);//在arr1中查找arr2第一次出现的位置,并返回arr1中第一个被查找的字符的首地址,将该地址打印后可打印出arr1从该字符起之后的字符串
	printf("%s\n", ret);//bcdef
	return 0;
}

Judge whether it can be found:

//strstr查找子串函数
#include<string.h>
int main()
{
	char arr1[20] = "abcdef";
	char arr2[20] = "bcde";
	char *ret = strstr(arr1, arr2);//在arr1中查找arr2第一次出现的位置,并返回arr1中第一个被查找的字符的首地址,将该地址打印后可打印出arr1从该字符起之后的字符串
	if (ret != NULL)
		printf("%s\n", ret);//bcdef
	else
		printf("找不到\n");
	return 0;
}

 

Simulation implementation (difficulty)

//strstr模拟实现
#include<assert.h>
char* my_strstr(const char *s1,const char *s2)
{
	assert(s1 && s2);//断言
	const char *cp = s1;

	if (*s2 == '\0')
		return (char*)s1;//特殊情况

	while (*cp)//判断*cp是否等于\0
	{
		const char *p1 = cp;
		const char *p2 = s2;
		while ((*p1) && (*p2) && (*p1 == *p2))
		{
			p1++;
			p2++;
		}
		if (*p2 == '\0')
			return (char*)cp;

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

 

2.strtok (cut string)

char * strtok ( char * str , const char * sep );
 
Parameters: 1. The string to be cut
           2. Segmentation mark
Return value: return the first address of the split string each time
 
The general usage is as follows:
//strtok
#include<string.h>
int main()
{
	char arr1[30] = "123456.3.haha@qq";
	char arr2[30] = { 0 };
	char p[30] = ".@";
	strcpy(arr2, arr1);//将arr1拷贝到arr2,使得arr1得以保留,我们操作arr2
	char *ret;//接收strtok函数的返回值,为一个指针
	//用循环的方式打印,第一次调用的时候第一个参数传参要传字符串,之后只用传空指针就可以
	for (ret = strtok(arr2,p); ret != NULL; ret = strtok(NULL, p))
	{
		printf("%s\n", ret);
	}
	return 0;
}

The weird thing about the strtok function is that when it is called for the first time, the first parameter is passed as a string, and then only a null pointer is passed. This is the result of static, which helps the strtok function to be retained after each call. The location of the original pointer, and the memory it opened up will not be destroyed with the end of the call.

 

Five, error reporting function

strerror

Function: Convert the error code to the corresponding error message, and return the starting address of the string corresponding to the error message

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_52454367/article/details/113893400