Detailed explanation of C/C++ character functions and string functions——String functions with limited 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        

2. String functions with limited length

2.1strncpy function

2.2 strncat function

2.3 strncmp function

2.4 Summary

3. Special string functions

3.1strstr function

3.2strtok function


1 Introduction        

        In the previous article, we talked about string functions with unlimited length, including strlen function, strcmp function, strcpy function, strcat function. Veterans who have learned it and those who have not learned it can click on characters with unlimited length . string function

Today we will talk about string functions with limited length, including strncpy function, strncat function, strncmp function. In these functions, the length limit requires us to add the number of bytes to be modified. There are also some special functions including strstr function and strtok function. Check out our content today.

2. String functions with limited length

2.1strncpy function

When we enter the cplusplus function, we can see the parameters of the strnpcy function.

        The return type of the strncpy function is char*, and its parameters are char * destination, const char * source, size_t num. Since the source does not need to be modified here, it just needs to be copied, so const is added. For size_num, it is necessary to Copy num bytes of the source string to destination, and modify the first byte. The detailed code is as follows:

#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "abcdefghigh";
	char brr[] = "zzzzz";
	strncpy(arr, brr, 4);
	printf("%s", arr);
	return 0;
}

 Let's run and see

We find that the first 4 bytes of the string arr are modified to the first 4 bytes of the string.

2.2 strncat function

Similarly, we enter the cplusplus function to view its parameters.

        The return value of the strncat function is char*, and its parameters are char * destination, const char * source, size_t num

and the strncpy function. Since the source function does not need to be modified, the const function is used to modify it. Size_t num is the number of characters that need to be connected. The detailed code is as follows:

#include <stdio.h>
#include <string.h>
int main()
{
	char arr[100] = "abcdefghigh";
	char brr[] = "xzzzzz";
	strncat(arr, brr, 4);
	printf("%s", arr);
	return 0;
}

When we run the code we can see:

Here, our string brr is source, connect its first 4 characters to the back of the string arr, and copy '\0' at the same time;

2.3 strncmp function

We enter the cplusplus website

        We can see that the parameters of strncmp are const char * str1, const char * str2, size_t num. Since str1 and str2 do not need to be modified, they only need to be compared, so they are modified with const. size_t is the individual string that needs to be compared. number. The return value of the strncmp function is of type int.

         If the first one is greater than the second one, return data greater than 0. If the first one is less than the second one, return data less than 0. If the first one is equal to the second one, return 0. Note that the comparison here is for the first num characters. For comparison, the comparison form is the same as the previous strcmp. The specific code demonstration is as follows:

#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "abcdefghigh";
	char brr[] = "abce";
	int ret=strncmp(arr, brr, 4);
	printf("%d", ret);
	return 0;
}

When we run it we can see:

The return value of the function is -1.

2.4 Summary

        For these three functions, they are similar to those with unlimited length, except that there is an additional restriction. This restriction is the restricted source. In other words, just copy as many num as possible, and the loop inside is caused by encountering '\ The while loop of 0' is changed to a for loop, and the judgment is i<num.

3. Special string functions

3.1strstr function

        The strstr function is a function that searches for substrings. It returns the position where the earliest string appears. Let’s go to the cplusplus website to view it.

        Let's look at the first one. Its parameters and return value are both modified with const. In fact, they are both the same, but the first one is modified with const so that its value cannot be changed, which is safer and prevents illegal modification. Let's take an example to experience the application of strstr function. The code is as follows:

#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "abcabcdefghigh";
	char brr[] = "abcd";
	char *crr=strstr(arr, brr);
	printf("%s", crr);
	return 0;
}

When we run it we can see:

We want to simulate the implementation, first draw its idea:

        That is, str2 and str1 record the position, s1 and s2 compare, if they are not equal, str1 and s1 move, if they are equal, str1 and str2 do not move, s1 and s2 move, if they are equal, they will point to str1 and str2 again, and then cycle. Until str1 encounters '\0', the simulation is implemented as follows:

#include <stdio.h>
char* my_strstr(char* arr, char* brr)
{
	char* str1 = arr, * s1 = arr, * str2 = brr, * s2 = brr;
	while (*str1)
	{
		if (*s2 == '\0')
		{
			return str1;
		}
		if (*s1 == *s2)
		{
			s1++;
			s2++;
		}
		else
		{
			str1++;
			s1 = str1;
			s2 = str2;
		}
	}
}
int main()
{
	char arr[] = "abcabcdef";
	char brr[] = "abcd";
	char* p = my_strstr(arr, brr);
	printf("%s", p);
	return 0;
}

3.2strtok function

        

        We can see that the return type of the strtok function is char*, and its parameters are, char * str, const char * delimiters. This function is string cutting and requires two strings. The second string appears in the first string. The string will be cut and point to the first character of this segment. The specific code is as follows:

#include <string.h>
#include <stdio.h>
int main()
{
	char arr[] = "abc,abcd@efacd*add";
	char brr[] = ",@*";
	char* str = arr;
	for (str = strtok(arr, brr); str != NULL; str = strtok(NULL, brr))
	{
		printf("%s\n", str);
	}
	return 0;
}

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/133439415