C/C++ character function and string function simulation implementation and detailed explanation——String function with unlimited length

Personal homepage : Click me to enter the homepage

Column classification: C language elementary       C language programming——KTV        C language mini-game      C language advanced

C language test questions

Everyone is welcome to like, comment and collect.

Work hard together and go to a big factory together.

Table of contents

 

1 Introduction 

2strlen function 

 3.strcpy function

4. strcat function

5.strcmp function


 

1 Introduction 

        For the string part, most of what we think of is the creation of strings, const modified strings, pointers to strings, character pointers, character arrays, etc. In fact, there are some character functions and string functions in the C language. Characters and strings are processed frequently in C language, but C language itself does not have a string type. Strings are usually placed in constant strings or character arrays. String constants are suitable for string functions that do not modify them.

2strlen function 

        For the strlen function, we can visit the cplusplus website cplusplus to check and see what the parameters of the strlen function are and what the return type looks like.b1d95859afe043a19e799ff492d654ef.png

         We see that the return type of the strlen function is size_t type, which is an unsigned integer, and its type is const char*str, which is the address of the string. Speaking of this, have you thought about our previous written test question, why do we put characters in the parameters? The element will report an error because the address of the character should be placed here. Since the string is stored in ASCLL code, the binary is converted to hexadecimal, and the integer is promoted, which will form a wild pointer and generate an error. This function counts the number of characters that end with '\0' and does not count '\0'; for example, if we want to calculate the length of a certain string, the code is as follows:

#include<stdio.h>
#include <string.h>
int main()
{
	char arr[] = "abcdef";
	size_t sz = strlen(arr);
	printf("%d\n", sz);
	return 0;
}

The running results are as follows:

1ee0e2a3a2314fb198b5f20660abb35e.png

We can simulate and implement a strlen function code as follows:

#include<stdio.h>
int my_strlen(const char* str)
{
	const char* p = str;
	while (*p != '\0')
	{
		p++;
	}
	return p - str;
}
int main()
{
	char arr[] = "abcdef";
	int ret = my_strlen(arr);
	printf("%d", ret);
	return 0;
}

        Here we mainly use pointers minus pointers to calculate the number of characters. Of course, we can also operate through counters or recursion, which will not be explained in detail here.


 3.strcpy function

        Let’s go to the cplusplus website to check the parameters of strcpy.

8afb6c22cee546d6b774e1aaa0899a5a.png

         We can see that the return type of the strcpy function is char* and the parameters are char * destination, const char * source

The first parameter is the pointer of the string you want to modify, and the second parameter is the pointer of the character you want to get. Since the second parameter does not need to be modified by us, errors will inevitably occur, so use const to modify it. Pay special attention to the fact that we need to ensure that the length of the source string must be greater than the length of the second string, otherwise it will cause a stack overflow problem and cause the program to crash. For example, we can write a program to experience the function of this function. The code is as follows:

#include<stdio.h>
#include <string.h>
int main()
{
	char arr[] = "abcdef";
	char arr1[] = "aaa";
	strcpy(arr, arr1);
	printf("%s\n", arr);
	return 0;
}

The running results are as follows:

3d08e896f8944990a3cedee949f9419f.png

We simulate the function strcpy, the code is as follows:

#include<stdio.h>
#include<assert.h>
void my_strcpy(char* str1, const char* str2)
{
	assert(str1 && str2);
	char* p = str1;
	while (*str2 != '\0')
	{
		*p = *str2;
		p++;
		str2++;
	}
}
int main()
{

	char arr[20] = { 'a','b','c'};
	const char arr2[] = "ba";
	my_strcpy(arr, arr2);
	printf("%s", arr);
}

        In this function, we first record the address of the first element of the string, then define the pointer for assignment, until the '\0' assignment is completed, and return the pointer of the first element.

4. strcat function

         We enter the cplusplus website to check the parameters of strcat

6a30913a45e54920be3f05ab0cc8939c.png

        The return type of this function is char*, and the parameters are char * destination, const char * source. Like the strcpy function, its first parameter is the pointer of the string you want to modify, and the second parameter is the character you want to get. Since the second parameter does not require us to modify the pointer, errors will inevitably occur, so use const to modify it. We want to concatenate strings, the code is as follows

#include<stdio.h>
#include <string.h>
int main()
{
	char arr[100] = { 'a','b'};
	char arr1[] = "aaa";
	strcat(arr, arr1);
	printf("%s\n", arr);
	return 0;
}

The running results are as follows:

1b652a2673864f9fa794f6593698e778.png

We simulate the strcat function, the code is as follows:

#include<stdio.h>
#include<assert.h>
void my_strcat(char* str1, const char* str2)
{
	assert(str1 && str2);
	char* p = str1;
	while (*p != '\0')
	{
		p++;
	}
	while (*str2 != '\0')
	{
		*p = *str2;
		p++;
		str2++;
	}
	*p = *str2;
}
int main()
{
	char arr[20] = { 'a','b','d','e','f' };
	const char arr2[] = "ghi";
	my_strcat(arr, arr2);
	printf("%s", arr);
	return 0;
}

        Here we just need a pointer to record the position of the string and move it until '\0' appears and then modify it. Note that the total length of the two strings needs to be less than the capacity of the first string.

5.strcmp function

69253ccff649474a92abf69e2c7a252f.png

        The return type of this function is int. Since two strings are compared, there is no need to modify the string, so it is modified with const.

9586c1451a804d05a2047a89f85b4b5a.png

        Its return value is an integer. If the first string is greater than the second string, it returns a number greater than 0. If it is less than the second string, it returns a number less than 0. If it is equal to it, it returns 0. The comparison is to compare one by one starting from the first one. If they are the same, then move back and compare. If they are different, compare and return the result. For example code:

#include<stdio.h>
#include <string.h>
int main()
{
	const char arr[] = "abcdefg";
	const char arr2[] = "abcdf";
	int ret = strcmp(arr, arr2);
	if (ret > 0)
	{
		printf(">");
	}
	else if (ret < 0)
	{
		printf("<");
	}
	else
	{
		printf("=");
	}
	return 0;
}

The running results are as follows:

cc359757c2e24ffe95312cc566039cb2.png

We simulate and implement strcmp, the code is as follows: 

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1&& str2);
	while (*str1 == *str2)
	{
		str1++;
		str2++;
	}
	return *str1 - *str2;
}
int main()
{
	const char arr[] = "abcdefg";
	const char arr2[] = "abcdf";
	int ret = my_strcmp(arr, arr2);
	if (ret > 0)
	{
		printf(">");
	}
	else if (ret < 0)
	{
		printf("<");
	}
	else
	{
		printf("=");
	}
	return 0;
}

As described above, first find different characters, subtract them, and then judge.

That’s the end of today’s content. I hope you can connect three times with one click.

 

Guess you like

Origin blog.csdn.net/Infernal_Puppet/article/details/133346000