Character function and string function analysis and simulation implementation

Character function and string function analysis and simulation implementation


insert image description here
insert image description here


1. A function to find the length of a string

1.1 shot

size_t strlen ( const char * str );
  • The string ends with '\0', and the strlen function returns the number of characters that appear before '\0' in the string (excluding '\0').
  • The string pointed to by the parameter must end with '\0'.
  • Note that the return value of the function is size_t, which is unsigned

1.2 strlen() simulation implementation

//模拟实现的strlen()函数命名位my_strlen()函数
//模拟实现方法一
//size_t my_strlen(const char* str)
//{
    
    
//	assert(str);
//	int count = 0;
//	while (*str != '\0')
//	{
    
    
//		count++;
//		str++;
//	}
//	return count;
//}

//模拟实现方法二:指针-指针
//size_t my_strlen(const char* str)
//{
    
    
//	assert(str);
//	char* start = str;
//	while (*str)
//	{
    
    
//		str++;
//	}
//	return str - start;
//}

//模拟实现方法三:递归
size_t my_strlen(const char* str)
{
    
    
	assert(str);
	if (*str == '\0')
		return 0;
	else
		return 1 + my_strlen(str + 1);
}

2. String functions with unlimited length

2.1strcpy

 char * strcpy ( char * destination, const char * source );
  • Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
  • The source string must end with '\0'
  • Will copy the '\0' in the source string to the target space.
  • The destination space must be large enough to accommodate the source string.

2.1.2 Simulation implementation

//模拟实现的strcpy()函数命名位my_strcpy()函数
char* my_strcpy(char* dest, const char* src)
{
    
    
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++)
	{
    
    
		;
	}
	return ret;
}

2.2 cracked

char * strcat ( char * destination, const char * source );
  • Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
  • The source string must end with '\0'.
  • Will copy the '\0' in the source string to the target space.
  • The destination space must be large enough to hold the contents of the source string.
  • The target space must be modifiable.

2.2.1 Simulation implementation

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;
}

2.3 strcmp

 int strcmp ( const char * str1, const char * str2 );
  • This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
  • The standard stipulates:
    . If the first string is greater than the second string, a number greater than 0 is returned.
    . Returns 0 if the first string is equal to the second string.
    . If the first string is less than the second string, returns a number less than 0.

2.3.1 Simulation implementation

//模拟实现的strcmp()函数命名位my_strcmp()函数
int my_strcmp(const char* str1, const char* str2)
{
    
    
	assert(str1 && str2);
	while (*str1 == *str2)
	{
    
    
		str1++;
		str2++;
	}
	return *str1 - *str2;
}



3. Length-restricted string functions

3.1 strncpy

 char * strncpy ( char * destination, const char * source, size_t num );
  • Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
  • Copies num characters from source string to destination space.
  • If the length of the source string is less than num, after copying the source string, add 0 after the target, until num

3.2 strncat

 char * strncat ( char * destination, const char * source, size_t num );
  • Appends the first num characters of source to destination, plus a terminating null-character.
  • If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

3.3 strncmp

int strncmp ( const char * str1, const char * str2, size_t num );
  • Compare until two characters are different or a string ends or all num characters are compared.
    insert image description here

4. String lookup

4.1 strstr

 const char * strstr ( const char * str1, const char * str2 );    
  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

4.1.2 Simulation implementation

//模拟实现的strstr()函数命名位my_strstr()函数
char* my_strstr(const char* str1, const char* str2)
{
    
    
	assert(str1 && str2);
	char* cp = str1;
	char* s1 = cp;
	char* s2 = str2;
	
	if (*s2 == '\0')
		return NULL;
	while (*cp)
	{
    
    
		//开始匹配
		s1 = cp;
		s2 = str2;
		while(*s1 && *s2 && *s1 == *s2)
		{
    
    
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return cp;
		cp++;
	}
	return NULL;
}

4.2 strtok

char * strtok ( char * str, const char * sep );
  • The sep parameter is a string defining the set of characters used as separators.
  • 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 mark in str, ends it with '\0', and returns a pointer to this mark. (Note:The strtok function will change the string being manipulated, so the string split using the strtok function is generally a temporary copy and can be modified
  • The first parameter of the strtok function is not NULL, the function will find the first mark 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 reserved position in the same string and search for the next token.
  • Returns NULL if there are no more tokens in the string.

4.2.1 How to use

int main()
{
    
    
	char arr1[] = "[email protected]";
	char copy[30];
	strcpy(copy, arr1);

	char arr2[] = "@.";
	char* ret = NULL;
	for (ret = strtok(copy, arr2); ret != NULL; ret = strtok(NULL, arr2))
	{
    
    
		printf("%s\n", ret);
	}
	return 0;
}

5. Error message reporting

When the library function is executed, if an error occurs, an error code will be stored in theerrnoin this variable. And errno is a global variable provided by C language.

5.1 string error

char * strerror ( int errnum );
  • Return the information corresponding to the error code.

5.1.1 How to use

#include <stdio.h>
#include <string.h>
int main()
{
    
    
	for (int i = 0; i < 10; i++)
	{
    
    
		printf("%d: %s\n", i,strerror(i));
	}
	return 0;
}

Running result:
insert image description here
character classification function:
insert image description here
character conversion:

int tolower (int c)
int toupper (int c)


6. Memory manipulation functions

6.1 memcpy

void * memcpy ( void * destination, const void * source, size_t num );
  • The function memcpy copies num bytes of data backwards from the location of source to the memory location of destination.
  • This function does not stop when encountering '\0'
  • If source and destination overlap in any way, the result of the copy is undefined

6.1.1 Simulation implementation

void* my_memcpy(void* dest, const void* src, size_t num)
{
    
    
	assert(dest && src);
	void* ret = dest;
	while (num--)
	{
    
    
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

6.2 memmove

void * memmove ( void * destination, const void * source, size_t num );
  • The difference from memcpy is that the source memory block and target memory block processed by the memmove function can overlap.
  • If the source space and the target space overlap, you have to use the memmove function to deal with it.

6.2.1 Simulation implementation

void* my_memmove(void* dest, const void* src, size_t num)
{
    
    
	assert(dest && src);
	void* ret = dest;
	if (dest > src)//从后向前拷贝
	{
    
    
		while (num--)
		{
    
    
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	else//从前向后拷贝
	{
    
    
		while (num--)
		{
    
    
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	return ret;
}

6.3 memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
  • Compare num bytes starting from prt1 and prt2 pointers
  • The return value is as follows:
    insert image description here

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/131880483