C language simulation to realize string processing function

It takes a little more courage to face the deteriorating self

Hello everyone, I am Ji Ning.

This article brings you the simulation implementation of the five major string processing functions.

Article directory

1. Simulation implementation of strlen function 

2. Simulation implementation of strcpy function

3. Simulation implementation of strcmp function

4. Simulation implementation of strcat function

5. Simulation implementation of strstr function


1. Simulation implementation of strlen function 

  The function of strlen is to find the length of the string

  The function prototype of strlen

normal method

  Find '\0', and pass the first address of the string. If the dereferenced value is not '\0', the pointer position will move backward, and the counter count will be increased by 1, knowing that the dereferenced value of the pointer is '\0' , the loop stops, and the function returns the value of the counter count

size_t my_strlen(char* str)
{
	size_t count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}

int main()
{
	char str[1000] = "I have a my_strlen hanshu";
	size_t len = my_strlen(str);
	printf("%zu", len);
	return 0;
}

pointer minus pointer

  After passing the address of the first element of the string, first save it with a character pointer, then move the saved pointer, find '\0', and after finding '\0', subtract the address of the first element of the string from the pointer at this position. string length

  The value obtained by subtracting the pointer from the pointer is the number of elements between the two pointers

size_t my_strlen(char* str)
{
	char* str2 = str;
	while (*str2 != '\0')
		str2++;
	return str2 - str;
}
int main()
{
	char str[1000] = "I have a my_strlen hanshu";
	size_t len = my_strlen(str);
	printf("%zu", len);
	return 0;
}

recursion

  The core of the recursive method is to make big things smaller

  But be aware that recursion must have a condition, and each recursion must get closer and closer to this condition

  When the passed pointer is dereferenced to '\0', it means that the recursion ends, otherwise, the pointer moves backward and continues recursion

size_t my_strlen(char* str)
{
	if (*str == '\0')
		return 0;
	else
		return my_strlen(str + 1) + 1;
}
int main()
{
	char str[1000] = "I have a my_strlen hanshu";
	size_t len = my_strlen(str);
	printf("%zu", len);
	return 0;
}

2. Simulation implementation of strcpy function

  The function of the strcpy function is to copy the string

  The function prototype of strcpy

Precautions:

  •   To copy a string, '\0' must also be copied 
  •   The target string must be variable, not a constant string
  •   The function returns the first address of the target string, so the first address must be saved first
#include<assert.h>
char* my_strcpy(char* str1, const char* str2)
{
	assert(str1 && str2);
	char* ret = str1;
	while (*str2 != '\0')
	{
		*str1 = *str2;
		str1++;
		str2++;
	}
	*str1 = *str2;
	return str1;
}
int main()
{
	char str1[100] = { "0" };
	char str2[100] = "my_strcpy hanshu";
	my_strcpy(str1, str2);
	return 0;
}

3. Simulation implementation of strcmp function

  The role of the strcmp function is to compare strings

  strcmp function prototype

 Precautions:

  When str1>str2 returns a number greater than 0, when str1<str2 returns a number less than 0, when str1=str2 returns 0, so when the simulation is implemented, when str1!=str2, you can use *str1-*str2 to Indicates the return value

//实现strcmp函数
#incldue<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 && *str2)
	{
		if (*str2 == *str1)
		{
			str1++;
			str2++;
		}
		else
			return *str1 - *str2;
	}
	if (*str1 == *str2 && *str1 == '\0')
		return 0;
}
int main()
{
	char str1[100] = "abcdefg";
	char str2[100] = "abcdefr";
	if (my_strcmp(str1, str2) > 0)
		printf("str1>str2\n");
	else
		printf("str2>str1\n");
	return 0;
}

4. Simulation implementation of strcat function

  The function of the strcat function is to realize the concatenation of strings

  strcat function prototype

   When implementing the strcat function, you must first find '\0', and then copy the string by imitating the implementation of the string copy function, and '\0' must also be copied

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* str1, const char* str2)
{
	assert(str1&&str2);
	char* ret = str1;
	//先找str1的  \0
	while (*++str1)
		 ;
	//将str2拷贝过来
	while (*str2 != '\0')
	{
		*str1 = *str2;
		str1++;
		str2++;
	}
	*str1 = *str2;
	return ret;
}
int main()
{
	char arr1[100] = "jining ";
	char arr2[100] = "bu shi da yuan zhong";
	my_strcat(arr1, arr2);
	printf("%s", arr1);
	return 0;
}

5. Simulation implementation of strstr function

  The function of the strstr function is to find a substring in a string

  strstr function prototype

To implement the strstr function, you need to set up two loops. The outer loop controls the pointer position of the target string, and judges whether there is a pointer dereferenced that is the same as the first character of the substring. If they are the same, then use the inner loop to judge whether the substring is part of the target string

#include<string.h>
char* my_strstr(const char* str1, const char* str2)
{
	char* str3 = str1;
	int i = 0;
	for (i = 0; i < strlen(str1); i++)
	{
		int count = 1;
		if (*(str3 + i) == *str2)
		{
			int j = 0;
			for (j = 1; j < strlen(str2); j++)
			{
				if (*(str3 + i + j) == *(str2 + j) && *(str2 + j) != '\0')
					count++;
			}
			if (count == strlen(str2))
				return (str1 + i);
		}
	}
	return NULL;
}
int main()
{
	char str1[100] = "zyb is a big yuanzhong is zyb TT";
	char str2[100] = "ong";
	char* ret = my_strstr(str1, str2);
	printf("%s", ret);
	return 0;
}

insert image description here

  The blogger has been writing for a long time, if you can give the blogger a free triple combo to encourage the blogger, then I think you are really hot in Thai pants! ! !

Guess you like

Origin blog.csdn.net/zyb___/article/details/131771667