In-depth exploration of string functions and mock implementations

 

Table of contents

 Foreword:

1. Introduction and simulation of common functions

1. strlen function

1.1 Introduction to the strlen function

1.2 Simulation implementation of strlen (three ways to achieve)

The first way: the way of the counter

The second way: recursive way

The third method: the pointer-pointer method

Two. strcpy function and strncpy function

2.1 Introduction of strcpy function 

2.2 Introduction of strncpy function 

2.3 Simulation implementation of strcpy

3. strcat function and strncat function

3.1 Introduction to the strcat function

3.2 Introduction of strncat function 

3.3 Simulation implementation of strcat function

Four. strcmp function and strncmp function

4.1 Introduction to strcmp function

4.2 Introduction of strncmp function

Five. strstr function

5.1 Introduction to the strstr function

5.2 Simulation implementation of strstr function

Summarize:


 Foreword:

The processing of characters and strings in C language is very frequent, but C language itself does not have a string type, and strings are usually placed in constant strings or character arrays ; string constants are used for those who do not modify it the string function; 

1. Introduction and simulation of common functions

 

1. strlen function

1.1 Introduction to the strlen function

~The string ends with '\0', and the strlen function returns the number of characters that appear before the string '\0' (excluding '\0');

The string pointed to by the parameter of the ~strlen function must end with '\0';

~The return value of the function is size_t, which is unsigned (special attention, error-prone); (the following code)

 

 analyze:

strlen(str1)=3;

strlen(str2)=6;

And 3-6=-3 should be a number less than 0, but the result is greater than; because the return value of the strlen function is unsigned, the unsigned minus the unsigned number, the result is also regarded as an unsigned number, and -3 as an unsigned number is a very large number;

1.2 Simulation implementation of strlen (three ways to achieve)

The first way: the way of the counter

int my_strlen(const char * str) {
 int count = 0;
 while(*str)
 {
 count++;
 str++;
 }
 return count; 
}

The second way: recursive way

int my_strlen(const char * str) {
 if(*str == '\0')
 return 0;
 else
 return 1+my_strlen(str+1);
}

The third method: the pointer-pointer method

int my_strlen(char *s) {
       char *p = s;
       while(*p != ‘\0’ )
              p++;
       return p-s; }

 

Two. strcpy function and strncpy function

2.1 Introduction of strcpy function 

The strcpy function copies the characters of the source string to the target space one by one;

The return value of the strcpy function here is the starting address of the target space, which is convenient for chain access;

Such as printf("%s\n", strcpy(arr1, arr2) );

~ The source string must end with '\0';

~ will copy the '\0' of the source string to the target space;

The target space must not be large enough to ensure that the source string can be stored; (if the target space is small, the source string cannot be placed, and an error will occur);

The target space must be mutable;

2.2 Introduction of strncpy function 

The strncpy function is similar to the strcpy function, except that the number of characters to be copied is added on the basis of the strcpy function, and a '\0' is placed at the end after copying;

For example: strncpy(arr1,arr2,4) is to copy 4 characters in arr2 to arr1;

           The function of strcpy(arr1,arr2) is to copy the entire string of arr2 to arr1;

2.3 Simulation implementation of strcpy

char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;           //记住目标空间的起始地址
	assert(dest != NULL);
    assert(src != NULL);

	while (*src!='\0')         
	{
       *dest=*src;
		dest++;
        src++;
	}

	return ret;                //返回目标空间的起始地址
}

3. strcat function and strncat function

3.1 Introduction to the strcat function

~ The source string must end with '\0';

~The target space must be large enough to accommodate the contents of the source string;

~ target space must be modifiable;

Note: A string cannot append a string to itself;

3.2 Introduction of strncat function 

The function of the strncat function is similar to the strcat function, except that the number of characters to be added is added on the basis of the strcat function; after the addition, put a '\0' at the end

For example: strcat(arr1, arr2) is to append the entire string in arr2 to arr1

           strncat(arr1,arr2,4) is to append 4 characters in arr2 to the back of arr1

3.3 Simulation implementation of strcat function

char* my_strcat(char* dest, const char* src) 
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	while (*dest)
	{
		dest++;
	}
	while ((*dest++ = *src++))
	{
		;
	}
	return ret;
}

Four. strcmp function and strncmp function

4.1 Introduction to strcmp function

 ~ The standard stipulates:

If the first string is greater than the second string, return a number greater than 0;

If the first string is less than the second string, return a number less than 0;

If the first string is equal to the second string, return 0 ;

How to judge the size?

The contents of two strings are compared one by one character by character, such as

str1  :   " asdfgh"        str2 :"asdagh"      

The first character is equal to the first character, and then compares the next character to know that a different character is encountered or '\0' is encountered (at least one string encounters '\0');

As in the above two strings, the first three characters are the same, compared to the fourth, f>a    

So str1>str2 as shown below

4.2 Introduction of strncmp function

The function of the strncmp function is similar to that of the strcmp function, except that the number of characters to be compared is added on the basis of the strcmp function;

For example: strcmp(arr1, arr2) is to compare the entire string of arr1 with the entire string of arr2

           The function of strncmp(arr1,arr2,4) is to compare the first 4 characters in arr1 with the first 4 characters in arr2

4.3 Simulation implementation of strcmp function

int my_strcmp(const char* str1, const char* str2) 
{
	assert(str1 != NULL);
	assert(str2 != NULL);
	while (*str1==*str2)        
	{
		if (*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}
	if (*str1 > *str2)
		return 1;
	else
		return -1;

}

Five. strstr function

5.1 Introduction to the strstr function

 The role of the function: (find a substring in a string)

Returns a pointer to the first occurrence of str1 in str2, or a null pointer if str2 is not part of str1.

The matching process does not include the terminating null character, but it stops there.

5.2 Simulation implementation of strstr function

char *  strstr (const char * str1, const char * str2) 
{
        char *cp = (char *) str1;
        char *s1, *s2;
        if ( !*str2 )
            return((char *)str1);
        while (*cp)
       {
                s1 = cp;
                s2 = (char *) str2;
                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;
                if (!*s2)
                        return(cp);
                cp++;
       }
        return(NULL);
}

Summarize:

1. The three functions of strcpy strcat strcmp pay special attention to '\0' and don't pay attention to the length of the string, so these three functions are called string functions with unlimited length;

2. strncpy strncat strncmp is a string function with limited length;   


If you think the article is good, I look forward to your one-click three-link, your encouragement is the source of motivation for my creation, let us cheer together, run together, and let us meet at the top!

 

Guess you like

Origin blog.csdn.net/2301_77509762/article/details/131900692