String + memory function

catalogue

Function introduction

Simulation Implementation of library function

Function introduction

strlen:

The string ends with '\ 0'. The strlen function returns the number of characters (excluding '\ 0') that appear before '\ 0' in the string.
The string pointed to by the parameter must end with '\ 0'.
Note that the return value of the function is size_t. Is unsigned. Unsigned number minus unsigned number or unsigned number

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

int main()
{
	if (strlen("abc") - strlen("abcdef") > 0) {
		printf(">");
	}
	else {
		printf("<=");
	}
	return 0;
}

strcpy:

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

The source string must end with '\ 0'.
The '\ 0' in the source string will be copied to the destination space.
The destination space must be large enough to hold the source string.
The target space must be variable.

strcat: add the characters of the original string to the end of the target string

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

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

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

int main()
{
	char arr1[30] = "hello";
	char arr2[] = "world";
	strcat(arr1, arr2);
	printf("%s", arr1);
	return 0;
}

 

strcmp: compare the character size at the corresponding position

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

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

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

int main()
{
	char arr1[] = "abedef";
	char arr2[] = "abc";
	int ret = strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}

 

strncpy:

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

Copy 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

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

int main()
{
	char arr1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
	char arr2[] = "hello";
	// The length of arr2 is less than num, which is supplemented by '\ 0'
	strncpy(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

 

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

int main()
{
	char arr1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
	char arr2[] = "hlo";
	// The length of arr2 is less than num, which is supplemented by '\ 0'
	strncpy(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

 

strncat: add num characters from source to destination, and add '\ 0' after it

char * strncat ( char * destination, const char * source, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "hello";
	char arr2[] = "world";
	strncat(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

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

int main()
{
	char arr1[] = "hello";
	char arr2[] = "world";
	strncat(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

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

int main()
{
	char arr1[30] = "hello********";
	char arr2[] = "worldaaaa";
	strncat(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

 

strncmp: compare the first num strings in str1 and str. if str1 is greater than str2, return a number greater than 0; If str1 is less than str2, the number less than is returned; If str1 equals str2, returns a number equal to 0

int strncmp ( const char * str1, const char * str2, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcqqqqqqqqqqq";
	int ret = strncmp(arr1, arr2, 3);
	printf("%d\n", ret);
	return 0;
}

 

strstr: find str1 in str2 and find the return address, otherwise NULL

char * strstr ( const char *str2, const char * str1);
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcdefghigklmn";
	char arr2[] = "efg";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL) {
		printf("can't find\n");
	} else {
		printf("%s\n", ret);
	}
	return 0;
}

 

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

int main()
{
	char arr1[] = "abcdefghigklmn";
	char arr2[] = "efgas";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL) {
		printf("can't find\n");
	} else {
		printf("%s\n", ret);
	}
	return 0;
}

 

strtok :

When the strtok function finds the first tag, the first parameter of the function is not NULL

When the strtok function finds a non first tag, the first parameter of the function is NULL

The sep parameter is a string that defines the set of characters used as delimiters
The first parameter specifies a string that contains 0 or more tags separated by one or more separators in the sep string.
The strtok function finds the next tag in str, ends it with \ 0, and returns a pointer to this tag (Note: the strtok function will change the string being operated on, so the string segmented by the strtok function is generally a temporary copy of the content and can be modified.)
The first parameter of the strtok function is not NULL. The function will find the first tag in str, and the strtok function will save its position in the string.
The first parameter of strtok function is NULL. The function will start at the position saved in the same string to find the next tag.
A NULL pointer is returned if there are no more tags in the string

char * strtok ( char * str, const char * sep );
#include <stdio.h>
#include <string.h>

int main()
{
	const char* p = "@.";
	char arr[] = "1234567@qq.com";
	char buf[50];
	strcpy(buf, arr);
	char* str = strtok(buf, p);
	printf("%s\n", str);
	str = strtok(NULL, p);
	printf("%s\n", str);
	str = strtok(NULL, p);
	printf("%s\n", str);
	return 0;
}

 

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

int main()
{
	const char* p = "@.";
	char arr[] = "1234567@qq.com";
	char buf[50];
	strcpy(buf, arr);
	char* str = NULL;
	for (str = strtok(buf, p); str != NULL; str = strtok(NULL, p)) {
		printf("%s\n", str);
	}
	return 0;
}

 

strerror: returns the address of the error message corresponding to the error code

When an error occurs when the library function is used, the global error message errno will be set as the error code generated by this execution of the library function

0 --- "No Error"

1 --- 

2 --- 

char * strerror ( int errnum );
#include <stdio.h>
#include <string.h>

int main()
{
	for (int i = 0; i < 10; i++) {
		printf("%s\n", strerror(i));
	}
	return 0;
}

Character classification function
functionReturns true if their parameters meet the following conditions
iscntrlAny control character
isspaceWhite space characters: space ',' page feed '\ f', line feed '\ n', carriage return '\ r', tab '\ t' or vertical tab '\ v'
isdigitDecimal digits 0 ~ 9
isxdigitHexadecimal digits, including all decimal digits, lower case letters A ~ F and upper case letters A ~ F
islowerSmall letters a ~ z
isupperCapital letters A ~ Z
isalphaLetters A ~ Z or A ~ Z
isalnumLetters or numbers, a ~ z, A ~ Z, 0 ~ 9
ispunctCharacters that are not alphanumeric or printable
isgrephAny graphic character
isprintAny printable character, including graphic characters and white space characters
tolowerConvert uppercase to lowercase
toupperConvert lowercase to uppercase

memcpy: void * can accept any type of pointer,

num bytes

void * memcpy ( void * destination, const void * source, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	int arr3[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int arr4[5] = { 0 };
	memcpy(arr4, arr3, sizeof(int) * 5);
	for (int i = 0; i < 5; i++) {
		printf("%d ", arr4[i]);
	}
	return 0;
}

memmove: move a part of the array with cross memory

void * memmove ( void * destination, const void * source, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	int arr[] = { 1, 2, 3, 4, 5 ,6 ,7 ,8, 9, 10 };
	memmove(arr + 2, arr, sizeof(int) * 5);
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

memcpy copies do not overlap, and memmove copies overlap

memcmp:

Compare num bytes after ptr1 and ptr2

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[] = { 1, 2, 3, 4, 5 };
	int arr2[] = { 1, 3, 3, 4, 5 };
	int ret = memcmp(arr1, arr2, 9);
	printf("%d\n", ret);
	return 0;
}

memset: fill count bytes in dest with character c

void *memset(void *dest, int c, size_t count);
#include <stdio.h>
#include <string.h>

int main()
{
	int arr[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	memset(arr, 0, sizeof(arr));
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

Simulation Implementation of library function

strlen

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

int my_strlen(const char* str)
{
	assert(str);
	if (*str == '\0') {
		return 0;
	} else {
		return 1 + my_strlen(++str);
	}
}

int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}
#include <assert.h>

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

int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}
#include <stdio.h>
#include <assert.h>

int my_strlen(const char* str)
{
	assert(str);
	char* p = str;
	while (*p != '\0') {
		p++;
	}
	return p - str;
}

int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}

strcpy

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

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

int main()
{
	char arr1[] = { 'a', 'b', 'c', 'd', 'e', 'f', '\0' };
	char arr2[20] = "aaaaaaaaaaaaaaaaaa";
	my_strcpy(arr2, arr1);
	printf(arr2);
	return 0;
}

strcat

char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	while (*dest) dest++;
	while (*dest++ = *src++);
	return ret;
}

int main()
{
	char arr1[30] = "hello";
	char arr2[] = "world";
	my_strcat(arr1, arr2);
	printf("%s", arr1);
	return 0;
}

 

strstr

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

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

int main()
{
	char arr1[] = "abcdefghigklmn";
	char arr2[] = "efgas";
	char* ret = my_strstr(arr1, arr2);
	if (ret == NULL) {
		printf("can't find\n");
	} else {
		printf("%s\n", ret);
	}
	return 0;
}

 

strcmp

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

int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (str1 == str2) {
		if (str1 == '\0') {
			return 0;
		}
		str1++;
		str2++;
	}
	if (*str1 > *str2) {
		return 1;
	}
	return -1;
}

int main()
{
	char arr1[] = "abedef";
	char arr2[] = "abc";
	int ret = my_strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}
#include <stdio.h>
#include <assert.h>

int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (str1 == str2) {
		if (str1 == '\0') {
			return 0;
		}
		str1++;
		str2++;
	}
	return *str1 - *str2;
}

int main()
{
	char arr1[] = "abedef";
	char arr2[] = "abc";
	int ret = my_strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}

memcpy

#include <stdio.h>

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

int main()
{
	int arr3[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int arr4[5] = { 0 };
	my_memcpy(arr4, arr3, sizeof(int) * 5);
	for (int i = 0; i < 5; i++) {
		printf("%d ", arr4[i]);
	}
	return 0;
}

memmove

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

void* my_memmove(void* dest, void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);
	if (dest < src) {
		while (num--) {
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	} else {
		while (num--) {
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	return ret;
}
// Dest SRC copy from front to back
// SRC dest copy back to front
int main()
{
	int arr[] = { 1, 2, 3, 4, 5 ,6 ,7 ,8, 9, 10 };
	my_memmove(arr + 2, arr, sizeof(int) * 5);
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

Keywords: C C++

Added by sBForum on Sat, 26 Feb 2022 21:47:43 +0200