The realization of the string function


Preface

Below I will introduce to you the implementation of some string functions.

First, the string left-handed problem

Example: cdefab is left-handed by abcdef.
The code is as follows (example):

#include<stdio.h>
#include<string.h>
int left_move(char *str1,char *str2)
{
    
    
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	if(len1 != len2)//二个字符串长度不相等,那么它就不是旋转的来,直接就返回0 
	{
    
    
		return 0;
	}
	strncat(str1,str2,6);
	char *ret = strstr(str1,str2);
	if(ret == NULL)
	{
    
    
		return 0;
	}
	else
	{
    
    
		return 1;
	}
}
int main(void)
{
    
    
	char arr1[] = "abcdef";
	char arr2[] = "cdefab";
	int ret = left_move(arr1,arr2);
	if(ret == 1)
	{
    
    
		printf("YES");
	}
	else
	{
    
    
		printf("NO");
	}
	return 0;
} 

Idea: Once the string is appended to itself, it becomes abcdefabcedf, and then strstr is used to determine whether cdefad is a substring of the changed string.
Note: strcat cannot append strings to itself in Visual Studio, but it can in dev c++.
To be on the safe side, add the length of the string after strncat.

Second, the realization of strlen function

Function prototype:

size_t __cdecl strlen(const char *_Str);

The code is as follows (example):

#include<stdio.h>
int my_strlen(char *str)
{
    
    
	int count = 0;
	while(*str !='\0')
	{
    
    
		count++;
		str++;//str++意思是它的地址加一,找到下一个字符的地址。 
	}
	return count;
}
int main(void)
{
    
    
	char arr[] = "abcdef";
	int len = my_strlen(arr);
	printf("%d",len);
	strlen
 } 

Three, the realization of strcpy function

Function prototype:

char * __cdecl strcpy(char * __restrict__ _Dest,const char * __restrict__ _Source);

The code is as follows (example):

#include<stdio.h>
#include<assert.h>
char *my_strcpy(char *dest,char *src)
{
    
    
	assert(dest != NULL && src != NULL);//保证传递进来的二个指针都不为空
	char *ret = dest;
	while(*src !='\0')//while循环里可简写成while(*dest++ == *src++) 
	{
    
    
		*dest = *src;
		dest++;
		src++;
	} 
	*dest = *src;//把src最后的'\0'拷贝到dest。 
	return ret;
}
int main(void)
{
    
    
	char arr1[] = "abcedf";
	char arr2[] = "bit";
	my_strcpy(arr1,arr2);
	printf("%s",arr1);
	
}

Fourth, the realization of strcat function

Function prototype:

char * __cdecl strcat(char * __restrict__ _Dest,const char * __restrict__ _Source);

The code is as follows (example):

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest,char* src)
{
    
    
	assert(dest != NULL);
	assert(src != NULL);
	char* ret = dest;
	while(*dest != '\0')//先找到目的地字符串的'\0',然后将src拷贝到dest。 
	{
    
    
		dest++;
	}
	while(*src !='\0')//while循环里可简写成while(*dest++ == *src++) 
	{
    
    
		*dest = *src;
		dest++;
		src++;
	} 
	*dest = *src;//把src最后的'\0'拷贝到dest。 
	return ret;
}
int main(void)
{
    
    
	char arr1[] = "abc";
	char arr2[] = "def";
	my_strcat(arr1,arr2);
	printf("%s",arr1);
}

Five, the realization of strcmp function

Function prototype:

int __cdecl strcmp(const char *_Str1,const char *_Str2);

The code is as follows (example):

#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* dest,const char* src)
{
    
    
	assert(dest && src);
	while(*dest == *src)
	{
    
    
		dest++;
		src++;
	}
	if(*dest>*src)
	{
    
    
		return 1;
	}
	else if (*dest<*src)
	{
    
    
		return -1;
	}
	else
	{
    
    
		return 0;
	}
}

int main(void)
{
    
    
	char arr1[] = "abcdef";
	char arr2[] = "abcdez";
	int ret = my_strcmp(arr1,arr2);
	if(ret == 1)
	{
    
    
		printf("字符串1大于字符串2。");
	}
	if(ret == -1)
	{
    
    
		printf("字符串1小于字符串2。");
	}
	if(ret == 0)
	{
    
    
		printf("字符串1等于字符串2。");
	}
 } 

to sum up

The above is the content to be talked about today. This article only briefly introduces the use of some string manipulation functions, and there are many functions that can be found and used on cplusplus.

Guess you like

Origin blog.csdn.net/qq_52208569/article/details/111143875