C language - detailed explanation of common character functions + simulation implementation

  Today is September 21, 2021. First of all, I wish you a Happy Mid Autumn Festival! May we all be blessed with longevity. Though far apart, we are still able to share the beauty of the moon together.. It's another full moon. I wish you all a reunion with the people you love and the people who love you on the Mid Autumn Festival~

Let's move on to the whole~

catalogue

1. Character function

strlen()

strcpy()

strcat()

strncpy()

strcmp()

strncat()

strncmp()

strstr()

strtok()

2. Summary

The following functions are library functions included in #include < string. H >. If you want to use them, you should first include this library

strlen()

1. Function: find the string length

  • The string ends with \ 0. The strlen() function returns the number of characters that appear before '\ 0' in the string (excluding \ 0)  

  • The string pointed to by the parameter must end with \ 0

  • The return type of the function is size_t. Is an unsigned number

2. Simulation Implementation:

Method 1: counter

size_t  my_strlen(const char* str)
{
	size_t count = 0;
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	char* str = "Mango";
	size_t count = my_strlen(str);
	printf("%u\n", count);
	return 0;
}

Method 2: pointer pointer

Pointer - pointer - > get the number of elements with the difference between them   Therefore, one tail pointer points to \ 0 and the other to the starting position. Subtracting the two will get the string len gt h

size_t my_strlen(const char* str)
{
	const char* start = str;
	const char* end = str;
	while (*end)
	{
		end++;
	}
	return end - start;
}
int main()
{
	char* str = "Mango";
	size_t count = my_strlen(str);
	printf("%u\n", count);
	return 0;
}

Method 3: recursion  

size_t my_strlen(const char* str)
{
    //If it does not point to \ 0, it will + 1 (the character itself points to), and then recurse to the next character
	if (*str)
	{
		return 1 + my_strlen(str + 1);
	}
    //str points to \ 0 and returns 0
	else
	{
		return 0;
	}
}
int main()
{
	char* str = "Mango";
	size_t count = my_strlen(str);
	printf("%u\n", count);
	return 0;
}

strcpy()

 

1. Function: copy string. strcpy: copies the contents of the source string from the beginning to the front of \ 0

Note:

  • The source string must end with \ 0
  • The \ 0 of the source string will be copied to the destination space

  • The destination space must be large enough to hold the source string

  • Target space must be variable

2. Simulation Implementation

  Note: the source string is not modified, so it is modified with const, the return type is char *, and the return is the starting address of the target space. Therefore, define a pointer to save the starting address, and then copy the content pointed to by src to dest

char* my_strcpy(char* dest,const char* src)
{
    assert(dest&&src);
    char* ret = dest;	//Save from address
    while(*src)
    {
        *dest = *src;
        dest++;
        src++;       
    }
    //The above only completes copying the contents before \ 0 in src
    //Also copy \ 0
    *dest = *src;
    return ret;
}
int main()
{
    char arr1[] = "xxxxxxx";
    char arr2[] = "Mango";
    char* ret = my_strcpy(arr1,arr2);
    printf("%s\n",arr1);
    printf("%s\n",ret);
    return 0;
}

Easy to write:

char* my_strcpy(char* dest,const char* src)
{
    assert(dest&&src);
    char* ret = dest;	//Save from address
    while(*dest++ = *src++)
    {
        ;
    }
    return ret;
}

The last time, src points to \ 0. After copying \ 0, while judges that the expression is false and jumps out of the loop.  

strcat()

1. Function: String append function, which appends the source string backward from the \ 0 position in the target space

matters needing attention:

  • The source string must end with \ 0

  • The target space must also be large enough to hold the contents of the source string

  • The target space must be modifiable

  • You can't add it to yourself!

2. Simulation Implementation

Idea: since it starts to append backward at the \ 0 position in the target space, it is necessary to find the \ 0 position in the target space first. Appending is similar to the strcpy process, appending character by character until it encounters the \ 0 of the source string

Note: the return is the starting address of the target space, so save its address with a pointer variable. The source string is not modified, so it is decorated with const

char* my_strcat(char* dest,const char* src)
{
    assert(dest && src);
	char* ret = dest;
	//1. Find the location of \ 0 in the target space
	while (*dest)
	{
		dest++;
	}
	//When jumping out of the loop, dest points to the \ 0 position of the target space, which is appended backward, equivalent to strcpy
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "Mango";
	char arr2[] = "Hello";
	my_strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

strncpy()

 strcmp()

1. Function: string comparison function. strcmp is a library function for comparing strings. It compares the contents of strings, not the length. The ascii code value of the first unequal character in two strings is compared, and the character before \ 0 is compared

  2. Simulation Implementation

Idea: traverse two strings, compare whether the characters at the corresponding positions are equal, find the first unequal character, dereference, and compare their corresponding ascii code values

Note: when the characters pointed to by the two are the same, judge whether one of them points to \ 0. If so, it means that the two strings are the same,

The two strings only need to be compared and do not need to be modified, so const is used

int my_strcmp(const char* s1,const char* s2)
{
    assert(s1 && s2);
    //When the characters pointed to by the two are the same, continue to find them until they are not equal, or one of them is \ 0, then jump out of the loop
    while(*s1 == *s2)
    {
        //First judge whether one of them is \ 0. If so, it means that the two strings are the same and both point to \ 0
        if(*s1 == '\0')
        {
            return 0;
        }
        s1++;
        s2++;
    }
    //Find the character that you don't want to wait for (or one is \ 0). Dereference and compare the corresponding value
    return *s1 - *s2;
}
int main()
{
	char arr1[] = "abcd";
	char arr2[]  = "";
	int ret = my_strcmp(arr1, arr2);
	if (ret > 0)
	{
		printf("arr1 > arr2\n");
	}
	else if (ret < 0 )
	{
		printf("arr1 < arr2\n");
	}
	else
	{
		printf("arr1 = arr2\n");
	}
	return 0;
}

strncat()

1. Function: String append function. Difference from strcat: this function can limit the number of bytes to be appended

  When the length to be appended is greater than (less than) the length of the source string, append to the source string \ 0 position to stop appending, and put \ 0 after appending

2. Simulation Implementation

  Idea:

1. Find the location of \ 0 in the target space

2. Copy n times in total. (Note: during the copy process, the energy string encounters \ 0 in advance, or the length to be copied is longer than the length of the source string)

3. Judge whether the count is still > 0. If it is still > 0, it indicates that \ 0 is encountered in advance, or the length of the source string is shorter than the length to be copied. In this case, add \ 0 at the next position after appending in the target space

char* my_strncat(char* dest, char* src, size_t count)
{
	assert(dest && src);
	char* tmp = dest;
	//1. Find the target space \ 0 location
	while (*dest!='\0')
	{
		dest++;
	}
	//When you jump out, dest points to \ 0 and starts copying from here
	while (count-- && (*dest++ = *src++))
	{
		;
	}
    //When the append is finished, the \ 0
     //After the while above is copied, dest points to the next location after the append is completed, where \ 0 is added
		*dest = '\0';
	return tmp;
}
int main()
{
	char arr1[20] = "Mango\0xxxxxx";
	char arr2[] = "Lemon";
	my_strncat(arr1, arr2, 2);
	printf("%s\n", arr1);
}

 strncmp()

  1. Function:

The function of strncmp is similar to that of strcmp, except that a parameter is added to determine the number of comparisons

The comparison shows that another character is different, or a string ends, or num characters are all compared

2. Simulation Implementation

int my_strncmp(char* dest, char* src, size_t count)
{
    assert(dest && src);
    //Find the unequal characters in the first count characters
    while (count)
    {
        count--;
        //The two points are equal, continue to look down
        if (*dest == *src)
        {
            //If they are equal and both are \ 0, 0 is returned
            if(*dest == '\0')
            {
                return 0;
            }
            dest++;
            src++;
        }
        else	//The two points are not equal and jump out of the loop
            break;
    }
    //When jumping out, find the characters you don't want to wait for, or the two are equal
    return *dest - *src;
}
int main()
{
    char* str1 = "abcdef";
    char* str2 = "abcd";
    int ret = my_strncmp(str1, str2, 3);
    if (ret > 0)
    {
        printf("str1>str2\n");
    }
    else if (ret < 0)
    {
        printf("str1 < str2\n");
    }
    else
    {
        printf("str1= str2\n");
    }
    return 0;
}

strstr()

1. Function:   Find a substring in a main string. If it is found, the substring is at the beginning of the main string anyway. If it is not found, a null pointer is returned

Note: the first occurrence is returned

2. Simulation Implementation

  Note: traverse two strings for comparison, and define three pointers. One pointer is used to traverse the main string, one pointer is used to traverse the string, and one pointer is used to save the position where the main string starts to traverse (it is convenient for the subsequent sequence to return the starting position of the substring in the main string)

When the substring goes to \ 0, it indicates that it is found,

When the main substring goes to \ 0, either the main string and substring are equal or cannot be found

When the substring is an empty string, the starting address of the main string is returned (as written in the library function)

  Illustration:

code:

//The return substring appears in the first position of the main string
char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	char* s1;	//Used to traverse str1
	char* s2;	//Used to traverse str2
	char* cp = str1;	//Used to save the position where the main character string starts traversal

	//If str2 is an empty string, the address of str1 is returned directly
	if (*str2 == '\0')
	{
		return str1;
	}
	//Traversal main string and substring comparison
	while (*cp)
	{
		s1 = cp;	//s1 backward comparison from cp position
		s2 = str2;	//s2 return to original position
		while ((*s1 == *s2)&&*s1&&*s2)	//Not only should they point to the same characters, but also s1 and s2 should not point to \ 0
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return cp;	//Returns the starting position of the substring in the main string
		}
		else
		{
			//s1 and s2 point to inequality, cp points to the next character, and then the next cycle assigns the value of cp to s1 for the next traversal
			cp++;
		}
	}
	//When jumping out of the while loop, it indicates that the main string has been traversed, indicating that it cannot be found - > NULL is returned
	return NULL;
}
int main()
{
	char* str1 = "MangoLemon";
	char* str2 = "an";
	char* tmp = strstr(str1, str2);
	char* tmpp = my_strstr(str1, str2);
	printf("%s\n", tmp);
	printf("%s\n", tmpp);
	return 0;
}

strtok()

1. Function: string cutting

  • The second parameter is a string that defines the set used as the split string

  • The strtok function finds the next tag in str, ends it with \ 0, and returns a pointer to this tag (Note: the strtok function will change the string to be manipulated, so the string segmented by the strtok function is generally a temporary copy of the content and can be modified)

  • When the first parameter of the strtok function is not NULL: the function will find the first tag in str, and the strtok function will save its position in the string

  • When the first parameter of strtok is NULL, the function will start at the position saved in the same string to find the next flag

  • If there are no more flags in the string, NULL is returned

  example:

 

 

int main()
{
    char arr1[] = "Mango@Lemon.Jiayou";
    char arr2[100] = {0};	//Prevent the strtok function from changing the data in arr1 and use the temporary array arr2
	char sep[] = "@.";	//The collection of split strings, that is, when @ or. Is encountered, the string is split
    strcpy(arr2,arr1);
    char* ret = NULL;
    //Split string
    //Note: the first pass parameter is arr2, and the subsequent pass parameters are NULL
    for(ret = strtok(arr2,sep); ret != NULL; ret = strtok(NULL,sep))
    {
        printf("%s\n",ret);
    }
    //amount to
    //char* ret2 =strtok(arr2, sep);
	//printf("%s\n", ret2);
	//ret2 =strtok(NULL, sep);
	//printf("%s\n", ret2);
}

  2. Summary

In the above functions,

strcpy()         strcat()          strcmp()  

The above three functions are not safe enough for string functions with unlimited length. Either append / copy until \ 0 is encountered, or compare until unequal characters are encountered

strncpy()   strncat()   strncmp()   

These three functions can control the number of bytes, which is relatively safe!

  This article will continue to update other character functions encountered by bloggers in the future ~ welcome to pay attention!

If you feel this article is helpful to you, please give the blogger a three company! Happy Mid Autumn Festival!

Keywords: C C++ data structure string

Added by rash on Tue, 21 Sep 2021 11:32:32 +0300