[C language] String function introduction and simulation implementation <1>

content

strlen

strcat

strcpy

strcmp

strstr


strlen

                           strlen
Header file #include <string.h> 
Format size_t strlen( const char* str)
Function Calculate string length
Return value Returns the length of the string
//strlen的模拟实现
#include<stdio.h>
#include<assert.h>
size_t  my_strlen(const char* str)
{
	assert(str); int count = 0;
	while (*str != '\0')
	{
		str++;
		count++;
	}
	return count;
}

strcat

                          strcat
Header file #include <string.h>  
Format char* strcat(char* dest,const char* src)
Function Appends a substring to the target string
Return value The first address of the target string
//strcat模拟实现
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* str1,const char *str2)
{
	assert(str1, str2);
	char* tmp = str1;
	//找出str1中的斜杠零
	while (*str1)
	{
		str1++;
	}
	//在str1上追加
	while (*str1++=*str2++)
	{
		;
	}
	return tmp;
}
int main()
{
	char arr1[20] = "hello";
	char arr2[] = " world";
	printf("%s",my_strcat(arr1,arr2));
	return 0;
}

strcpy

                          strcpy
Header file #include <string.h> 
格式 char * strcpy (char * dest, char * src)
Function Copy the parameter src string to the address pointed to by the parameter dest
Return value The first address of the target string
//模拟实现strcpy
#include<stdio.h>
#include<assert.h>
char* my_strcpy(char *str2,char *str1)
{
	assert(str1, str2);
	char* tmp = str2;
	while (*str1)
	{
		*str2++ = *str1++;
	}
	return tmp;
}
int main()
{
	char arr1[20] = "abcdef";
	char arr2[20] = "xxxxxxxxxxxxx";
	my_strcpy(arr2,arr1);
	printf("%s", arr2);
	return 0;
}

strcmp

                         strcmp
Header file #include <string.h> 
Format int strcmp (const char* str1,const char* str2)
Function Compare the size of two strings

Return value str1>str2 Return value>0           

                           str1<str2 return value<0     

                           str1=str2 return value=0     

//strcmp模拟实现
#include<stdio.h>
#include<assert.h>
int strcmp(const char* str1,const char* str2)
{
	assert(str1, str2);
	while (*str1 == *str2)
	{
		str1++;
		str2++;
	}
	return *str1 - *str2;
	
}
int main()
{
	char arr1[] = "abc";
	char arr2[] = "abcd";
	printf("%d", strcmp(arr1, arr2));
	return 0;
}

strstr

                          strstr
Header file #include <string.h> 
格式                   char* my_strstr(const char*str,const char* substr)
Function Find substring in target string
Return value In the target string, the first address of the string that is the same as the substring
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char*str,const char* substr)
{
	char* s1 = str;//防止str被破坏
	char* s2 = substr;//防止substr被败坏
	char* cur = str;//存储中间地址
	assert(str && substr);
	//排除substr为空
	if (substr == '\0')
	{
		return str;
	}
	while (*cur)
	{
		s1 = cur;
		s2 = substr;
		while (*s1 && *s2 && *s1 == *s2)
		{
			s1++;
			s2++;
	    }
		if (*s2 == '\0')
			return cur;
		cur++;
	}
	return NULL;
}
int main()//模拟实现strstr函数
{
	char arr1[] = "abbbcdef";
	char arr2[] = "bbc";
	printf("%s", my_strstr(arr1, arr2));
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_64332179/article/details/122986054