Detailed explanation of character functions and string functions (including simulation implementation)

content

1. Find the string length function

 1.1 strlen

2. String functions with unlimited length

 2.1 strcpy

 2.2 strcmp

 2.3 strcat

3. Length-limited string functions

 3.1 strncoy

 3.2 strncmp

 3.3 strncat

 4. String lookup

 4.1 strstr

 4.2 strtok

5. Memory operation function

 5.1 memcpy

 5.2 memmove

 5.3 memcpy


1. Find the string length function

 1.1 strlen

size_t strlen ( const char * str );

Used to find the length of the string.

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'.
The return value of the function is size_t, which is the number of strings and is an unsigned integer.

Example of use:

#include<stdio.h>
#include<string.h>
int main() 
{
	char str[] = "duachebdbvla";
	int sz = sizeof(str);
	printf("%d", sz);
	return 0;
}

Mock implementation of strlen :

int my_strlen(const char* str)
{
	assert(str);
	int i = 0;
	while (*str++)
	{
		i++;
	}
	return i;
}


2. String functions with unlimited length

 2.1 strcpy

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

Used to copy strings.

The source string must end with '\0'.
Will copy '\0' in the source string to the destination space.
The destination space must be large enough to hold the source string.
The target space must be mutable.

Example of use:

#include<stdio.h>
#include<string.h>
int main() 
{
	char str1[20] = "duachebdbv";
	char str2[20] = "0";
	strcpy(str2, str1);
	printf(str2);
	return 0;
}

Mock implementation of strcpy:

char* my_strcpy(char*des,const char*src)
{
	char* tmp = des;
	assert(des && src);
	while (*des++ = *src++;)
	{
		;
	}
return tmp;
}

 2.2 strcmp

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

Used to compare the size of strings.

If the first string is greater than the second string, return a number greater than 0. If the
first string is equal to the second string, return 0. If
the first string is less than the second string, return a number less than 0. number

 

Example of use:

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[20] = "asdfghj";
	char str2[20] = "awsdefghjjjk";
	int ret=strcmp(str1, str2);
	if (ret > 0)
	{
		printf("str1>str2");
	}
	else if (ret == 0)
	{
		printf("str1=str2");
	}
	else
	{
		printf("str1<str2");
	}
	return 0;
}

Note: Because strcmp does not compare the length of the string, but compares the ascii code value of the characters corresponding to the same subscript, if the characters are the same, compare the next one until the two are different. This code is because str1[0] and str2[0] are both a, so compare the next character, str1[1] is 's', str2[1] is 'w', and the ascii code value of s is less than the ascii value of w , so an integer less than 0 is returned.

Mock implementation:

int my_strcmp(const char* str1, const char* str2)

{
	assert(*str1 && *str2);
	while (*str1 || *str2)
	{
		if (*str1 == *str2)
		{
			str1++;
			str2++;
		}
		else
		{
			return *str1 - *str2;
		}
	}
	return 0;
}


 2.3 strcat

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

Copies one string to the end of another string.

The source string must end with '\0'.
The destination space must be large enough to hold the contents of the source string.
The target space must be modifiable.

Example of use:

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[20] = "kajhcjeef";
	char str2[40] = "jdsafbkw";
	strcat(str2, str1);
	printf(str2);
	return 0;
}


Mock implementation:

char* my_strcat(char* des, const char* scr)
{
	char* tmp = des;
	assert(des && scr);
	while (*des)
	{
		des++;
	}

	while (*des++=*scr++)
	{
		;
	}
	return tmp;
}

The implementation of the above three functions, strcoy, strcmp, and strcat, is nothing more than stopping when '\0' or comparing the size of two characters, which has nothing to do with the length of the string, so we can think that these three functions are unlimited in length. of. The next strncoy, strncmp, strncat are related to the length.

3. Length-limited string functions

 3.1 strncoy

char * strncpy ( char * destination, const char * source, size_t num );

 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 source string, append 0 to the end of the target until num.

Example of use:

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[20] = "kajhcjeef";
	char str2[40] = "jdsafbkw";
	strncpy(str2, str1,3);
	printf(str2);
	return 0;
}

 Mock implementation:

char* my_strncpy(char* des, const char* scr, int num)
{
	char* ret = des;
	assert(des && scr);
	while (num--&&*scr&&*des)
	{
		*des++ = *scr++;
	}
	return ret;
}

 3.2 strncmp

int strncmp ( const char * str1, const char * str2, size_t num );

Used to compare the size of the first num characters of two strings.

 The comparison stops when two characters are different, or when a string ends, or when the number of comparison characters is num. When the string composed of the first num characters of str1 is greater than the string composed of the first num characters of str2, it returns an integer greater than 0, returns 0 when it is equal, and returns an integer less than 0 when it is less than.

Example of use:

#include<string.h>
#include<stdio.h>
int main()
{
	char str1[20] = "jajhcjeef";
	char str2[40] = "jasafbkwn";
	int ret=strncmp(str1, str2, 3);
	printf("%d", ret);
	return 0;
}

Mock implementation:

int my_strncmp(const char* str1, const char* str2, int num)
{
	assert(str1 && str2);
	while (num--&&(*str1||*str2))
	{
		if (*str1 != *str2)
		{
			return *str1- *str2;
		}
		else
		{
			str1++;
			str2++;
		}
	}
	return 0;
}

 3.3 strncat

char * strncat ( char * destination, const char * source, size_t num );

 Used to append strings.

Copies the first num characters of the string source to the end of the string destination (starting with '\0').

Example of use:

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[20] = "jajhcjeef";
	char str2[40] = "ja";
	strncat(str2, str1, 6);
	printf(str2);
	return 0;
}

Mock implementation:

#include<stdio.h>
#include<assert.h>
char* my_strncat(char* des, const char* scr, int num)
{
	char* ret = des;
	assert(des && scr);
	while (*des)
	{
		des++;
	}
	while (num && ((*des++ = *scr++) != '\0'))
	{
		num--;
	}
	if (num == 0)
		*des = '\0';
	return ret;
}

 4. String lookup

 4.1 strstr

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

Find the string str2 in the string str1, and if found, return the starting address of the same fragment string in the string str1 as the string str2.

Example of use:

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[20] = "jajhcjeef";
	char str2[40] = "cj";
	char* tmp = strstr(str1, str2);
	printf("%s",tmp);
	return 0;
}

Mock implementation:

#include<stdio.h>
//#include<string.h>
#include<assert.h>
char* my_strstr(const char* str, const char* substr)
{
	const char* s1 = str;
	const char* s2 = substr;
	const char* cur = str;

	assert(str && substr);
	if (*substr == '\0')
	{
		return (char*)str;
	}
	while (*cur)
	{
		s1 = cur;
		s2 = substr;
		while (*s1 &&  *s2 && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return (char*)cur;

		cur++;
	}
	return NULL;
}

 4.2 strtok

char * strtok ( char * str, const char * sep );

Used to split a string according to a user-specified delimiter.
The first argument specifies a string containing zero or more tokens separated by one or more delimiters in the sep string
. The second parameter sep is a string that defines the set of characters used as separators .
The strtok function finds the next token in str, ends it with \0, and returns a pointer to this token . (Note:
The strtok function will change the string being manipulated, so the string segmented by the strtok function is generally the content of a temporary copy
and can be modified.)
The first parameter of the strtok function is not NULL, the function will find the str The first token in the strtok function will save its
position in the string.
The first argument to 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.

Example of use:

#include<stdio.h>
#include<string.h>
int main()
{
	char str1[50] = "zhouling#123.b@uo";
	const char sep[10] = "#.@";
	char arr[50];
	char* ch = NULL;
	strcpy(arr, str1);
	for (ch = strtok(arr, sep);ch != NULL; ch = strtok(NULL, sep))
	{
		printf("%s\n", ch);

	}
	return 0;
}

5. Memory operation function

 5.1 memcpy

void *memcpy( void *dest, const void *src, size_t count );

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 it encounters '\0'. This function is somewhat similar to strcmp, but this function can copy integers or other types of data.
If there is any overlap between source and destination, the result of the copy is undefined.

Use example 1:

#include<stdio.h>
int main()
{
	char str1[] = "zhouling";
	char str2[] = "mlanvkml";
	int arr1[4] = { 1,2,3,4 };
	int arr2[4] = { 5,6,7,8 };
	int i = 0;
	char*str = memcpy(str2, str1, sizeof(str1[0]) * 2 );
	int* arr = memcpy(arr1, arr2, sizeof(arr1[0]) * 2);
	for (i = 0; i < 4; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	printf("%s", str);
	return 0;
}


 Use example 2:

#include<stdio.h>
#include<string.h>
struct PeoPle
{
	char name[20];
	int age;
}People1,People2;
int main()
{
	char myname[] = "zhouling";
	People1.age = 18;
	memcpy(People1.name, myname, sizeof(myname)+1);
	memcpy(&People2, &People1, sizeof(People1));
	printf("People2:%s %d", People2.name, People2.age);
	return 0;
}

 

 

Mock implementation:

void* my_memcpy(void* dst, const void* src, size_t count)

{

    void* ret = dst;

    assert(dst&&src);

    while (count--) 
    {

        *(char*)dst = *(char*)src;

        dst = (char*)dst + 1;

        src = (char*)src + 1;
    
    }

    return ret;
}

 5.2 memmove

void * memmove ( void * destination, const void * source, size_t num );

The function is basically the same as memcpy, but the difference with memcpy is that the source memory block and the target memory block processed by the memmove function can overlap.
If the source space and the target space overlap, it is best to use the memmove function to deal with it.

Example of use:

#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "I don't love you and you like.......me";
	memmove(str + 25, str + 2,11);
	puts(str);
	return 0;
}

 Mock implementation:

void * my_memmove(void* des, const void* scr, int num)
{
	void* ret = des;
	assert(des && scr);
	while (num--)
	{
		if (des < scr)
		{
			*((char*)des)++ = *((char*)scr)++;
		}
		else
		{
			*((char*)(des)+num) = *((char*)(scr)+num);
		}
	}
	return ret;

}

 5.3 memcpy

int memcmp( const void *buf1, const void *buf2, size_t count );

Compare the num bytes starting from the ptr1 and ptr2 pointers , if the former is greater than the latter, an integer greater than 0 is returned, otherwise, an integer less than 0 is returned, and 0 is returned if they are equal.

 Example of use:

#include <stdio.h>
#include <string.h>
int main()
{
	char buffer1[] = "zhouzhou";
	char buffer2[] = "zhdhZhou";
	int n;
	n = memcmp(buffer1, buffer2, 2);
	if (n > 0) printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (n < 0) printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
	return 0;
}

Mock implementation:

int my_memcmp(const void* buf1, const void* buf2, size_t count)
{
	void* tmp1 = buf1;
	void* tmp2 = buf2;
	assert(buf1 && buf2);
	while (count--)
	{
		if (*((char*)tmp1) != *((char*)tmp2))
		{
			return *((char*)tmp1) - *((char*)tmp2);
		}
		((char*)tmp1)++;
		((char*)tmp2)++;
	}
}

Alright, that's the end of today's study notes sharing, please give a like if you see it here

 

Guess you like

Origin blog.csdn.net/m0_63039919/article/details/123138585