Detailed explanation and simulation of str-function (strlen, strcpy, strcat, strcmp)

String built-in functions

       In addition to the basic data types mentioned before, there are two commonly used structures in the C language: arrays and strings. In fact, the C language itself does not have a string type, and strings are usually placed in commonly used strings or character arrays. String constants are suitable for string functions that do not change them. Strings usually need to end with'\0'. For the unique structural characteristics of strings, the string library provides a series of functional library functions. The following describes how to use some basic built-in functions and how to write simulation functions.

Function and simulation

strlen

Introduction to strlen function

       The main function of strlen is to find the length of the string. Of course, it starts from the first element of the character length and ends at'\0', but it does not include'\0'. This point requires special attention. The following declaration of its function in MSDN:

size_t strlen( const char *string );

       This function requires a character pointer to be passed in, and the content cannot be modified, and returns a size_t (ie unsigned int) type value, that is, the length.
       For example, the following code:

char arr[] = "abcdefg";
	int length = strlen(arr);
	printf("the arr length is %d\n", length);

       The value of its operation is as we need to calculate, a total of seven characters in length:
Insert picture description here

Strlen function simulation

       The specific code is as follows:

int my_strlen(const char *str)//strlen的模拟实现
{
    
    
	const char *str_c = str;//保护指针
	int count = 0;
	assert(str != NULL);//断言,保证指针不为空
	while (*str_c != '\0')
	{
    
    
		count++;
		str_c++;
	}
	return count;//返回长度的数值
}

       Through the first address of the string, access its elements in sequence according to the characteristics of its continuous storage until the end of'\0'.

strcpy

Introduction to strcpy function

       The main function of strcpy is to completely copy the contents of a string into the target space, including'\0', which requires that the target space has enough space to receive the source string. The declaration of its function in MSDN is as follows:

char *strcpy( char *strDestination, const char *strSource );


       It requires two addresses to be passed in. The first is the first address of the target string, and the second is the source address that needs to be copied. It requires that the data in the source address can only be accessed and cannot be modified, and the same address is returned.
       E.g:

	char arr[100] = "abcdefg";
	char arr_s[] = "strcpy function";
	char* pp= strcpy(arr,arr_s);
	printf("the final string is >%s\n", pp);

       The output value is:
Insert picture description here

strcpy function simulation

char *my_strcpy(char *strD, const char *strS)
{
    
    
	assert(strD != NULL && strS != NULL);
	char* strd = strD;
	const char* strs = strS;
	while (*strs != '\0')
	{
    
    
		*strd++ = *strs++;
	}
	*strd = '\0';
	return strD;
}

strcat

Introduction to strcat function

       The main function of this function is to realize the splicing of two strings. MSDN gives a statement:

char *strcat( char *strDestination, const char *strSource );

       The parameter types and return values ​​will not repeat the
       program functions such as:

char arr[100] = "I like ";
	char arr_s[] = "strcat function";
	char* pp= strcat(arr,arr_s);
	printf("the final string is >%s\n", pp);

       Procedure result:
Insert picture description here

strcat function simulation

char *my_strcat(char *strD, const char *strS)
{
    
    
	assert(strD != NULL && strS != NULL);
	char* strd = strD;
	const char* strs = strS;
	while (*strd != '\0')
	{
    
    
		strd++;
	}
	while (*strs != '\0')
	{
    
    
		*strd++ = *strs++;
	}
	*strd = '\0';
	return strD;
}

strcmp

strcmp function

       On Baidu Encyclopedia, we can clearly find out the function of strcmp: the strcmp function is the abbreviation of string compare (string compare), which is used to compare two strings and return an integer according to the result of the comparison. The basic form is strcmp(str1,str2). If str1=str2, it returns zero; if str1<str2, it returns a negative number; if str1>str2, it returns a positive number. With the basis and standard of this kind of judgment, you can write the simulation function yourself.
First look at the usage method given by the library function:

int main()
{
    
    
	char str[] = "wocai xuexi hhhhh";
	char str1[] = "who like c";
	int res = strcmp(str, str1);
	if (res > 0)
		printf("str>str1\n");
	else if (res < 0)
		printf("str<str1\n");
	else
		printf("str=str1\n");
	return 0;
}

       The output is as follows:
Insert picture description here

strcmp function simulation

int my_strcmp(const char *str1, const char *str2)//strcmp的模拟实现
{
    
    
	assert(str1 != NULL && str2 != NULL);
	const char* s1 = str1;
	const char* s2 = str2;
	int result;
	while (*s1 != '\0' || *s2 != '\0')
	{
    
    
		if (*s1 - *s2 != 0)
			break;
		s1++;
		s2++;
	}
	result = *s1 - *s2;
	return result;
}

Guess you like

Origin blog.csdn.net/dream_i_success/article/details/110112455