Character function and string function strcpy strcmp strstr memmove

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


Preface

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

One, strlen

The function returns the number of characters that appear before the'\0' in the string, and the string pointed to by the parameter must end with'\0'. The following is the simulation implementation:

#include<stdio.h>
#include<assert.h>
#include<string.h>
//模拟实现strlen
int my_strlen(const char *string){
    
    
	assert(string != NULL);
	int count = 0;
	const char *p = string;
	while (*p++ != '\0'){
    
    
		count++;
	}
	return count;
}
void main(){
    
    
	char *str = "abcdef";
	printf("%d\n", my_strlen(str));
}

Two, strcpy

There are two parameters to assign the value of the first to the first string. The source string must end with'\0', and the'\0' in the source string will be copied to the target space. The target space must be sufficient Large to ensure that the source string can be stored. The mode is implemented as follows:

#include<stdio.h>
#include<assert.h>
#include<string.h>
//模拟实现strcpy
char * my_strcpy(void *dest, const void *src)
{
    
    
	assert(dest!=NULL && src!=NULL);
	char *pdest = (char *)dest;
	const char *psrc = (const char *)src;
	while (*psrc != '\0'){
    
    
		*pdest++ = *psrc++;
	}
	pdest= '\0';
	return dest;
}

void main(){
    
    
	char *str = "abcdse";
	char arr[20] = {
    
    0};
	printf("%s\n", my_strcpy(arr, str));
}

Three, strcmp

Comparison function, to compare the size of strings, this function starts to compare the first character of each string. If they are equal, it continues to compare with the following until the characters are different, or until the terminating null character is
reached. (Note that although the ASCII comparison is used here, the ASCII codes of all characters are not added up, and counted as a whole. At the beginning, I think so, but strcmp compares characters one by one. Once the result is out, it will end immediately and return )

#include<stdio.h>
#include<assert.h>
#include<string.h>
//模拟实现strcmp
int my_strcmp(const char *string1, const char *string2)
{
    
    
	assert(string1 != NULL && string2 != NULL);
	const char *ps1 = string1;
	const char *ps2 = string2;

	while (*ps1 != '\0' && *ps2 != '\0')
	{
    
    
		if (*ps1 > *ps2)
			return 1;
		else if (*ps1 < *ps2)
			return -1;
		ps1++;
		ps2++;
	}

	if (*ps1 != '\0')
		return 1;
	if (*ps2 != '\0')
		return -1;
	return 0;
}

void main()
{
    
    
	char *str1 = "Hello";
	char *str2 = "HelloAAA";
	int ret = my_strcmp(str1, str2); //<0 ==0 >0
	if (ret == 0)
		printf("str1 == str2");
	else if (ret < 0)
		printf("str1 < str2");
	else
		printf("str1 > str2");
}

Four, strcat

"Append" the source character to the target, plus a terminating null character. The source string must end with'\0', the target space must be modifiable, and the space must be large enough.

#include<stdio.h>
#include<assert.h>
#include<string.h>
//模拟实现strcat
char *my_strcat(void *dest, const void *src)
{
    
    
	assert(dest != NULL &&src != NULL);
	char *pdest = (char *)dest;	
    char *psrc = (const char *)src;
	while (*pdest){
    
    
		pdest++;
	}
	while ((*pdest++ = *psrc++));
	return dest;
}

void main(){
    
    
	char *str = "bcdef";
	char ar[20] = {
    
    'a'};
	printf("%s\n", my_strcat(ar, str));
}

Five, strstr

Returns a pointer to the first occurrence of str2 in str1. If str2 is not part of str1, returns a null pointer.

#include<stdio.h>
#include<assert.h>
#include<string.h>
//模拟实现strstr
char *my_strstr(const char*src, const char*dest)
{
    
    
	assert(dest != NULL &&src != NULL);
	char *pdest = (const char *)dest;
	char *psrc = (const char *)src;
	char *s1 = (const char *)src;
	if (*pdest == '\0')
		return NULL;
	while (*psrc){
    
    
		psrc = s1;
		while (*pdest && *psrc && (*pdest == *psrc)){
    
    
			pdest++;
			psrc++;
		}
		if (*pdest == '\0')
			return s1;
		s1++;
	}
	return NULL;
}

void main(){
    
    
	char str[20] = "abcdef";
	char ar[20] = "bc";
	printf("%s\n", my_strstr(str, ar));
}

Six, memmove

Similar to the memcpy function, the difference is that it can handle the overlap of the source memory block and the target memory block.

#include<stdio.h>
#include<assert.h>
#include<string.h>
//模拟实现memmove
void* my_memmove(void *dest, const void *src, size_t count)
{
    
    
	assert(dest != NULL && src != NULL);
	char *pdest = (char *)dest;
	const char *psrc = (const char *)src;

	if (psrc >= pdest || pdest >= psrc + count)
	{
    
    
		while (count-- > 0)
		{
    
    
			*pdest++ = *psrc++;
		}
	}
	else
	{
    
    
		//存在内存重叠
		psrc = psrc + count - 1;
		pdest = pdest + count - 1;
		while (count-- > 0)
		{
    
    
			*pdest-- = *psrc--;
		}
	}

	return dest;
}
void main()
{
    
    
	char str[20] = "abcdefghijk";
	char st[20]="acsxd";
	printf("str = %s\n", str);
	memmove(st, str, 4);
	//my_memmove(str + 2, str, 4);
	printf("str = %s\n", st);
	printf("str = %s\n", str);
}

When the function that determines the overlap is canceled, it is the same as the memcpy function. So I won't review the memcpy function here.

to sum up

In addition to these functions, there are strncpy, strncat, strncmp, and similar functions. In addition to the source and target, the parameters of these functions are length, so it is relatively simple to simulate and implement, which is to change the conditions of the while loop. The success is related to this length (count–>0).

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/110008936