In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian introduces in detail "the function of string library function in C++ and how to use it" with detailed contents, clear steps and proper handling of the details. I hope that this article "what is the role of string library function in C++ and how to use it" can help you solve your doubts.
What is the 1.strlen function?
The strlen function is the most common function in the string header file, which is used to find the length of a string. The full name of strlen in English is string length, which is the length of a string and can be remembered as its name implies.
Specific usage:
✅ this is an introduction intercepted from C++ reference. The use of the strlen function is to pass it the address of the first element of a string array, and its return value is the number of characters.
The ⭕ demo code is as follows:
# includeint main () {char str [] = "abc"; int len1 = strlen (str); int len2 = strlen ("abc"); printf ("% d\ n% d", len1,len2); return 0;}
!! It should be noted here that "abc" also represents an array of strings, which is passed to the strlen function with the address of the first element.
Running result:
Let's try to run this code:
# includeint main () {char str1 [] = "abc"; int len1 = strlen (str1); char str2 [] = {'a'axie gramma str2; int len2 = strlen (str2); printf ("% d\ n% d", len1,len2); return 0;}
Running result:
❓ Why is the length of str2 here 13? At first glance, isn't it three elements of abc, with a length of 3?
There is a point of knowledge that needs to be added here. In C, the system adds a'\ 0' at the end of the string array as the closing flag. The strlen function works by calculating the number of elements before'\ 0' in the incoming array (the array pointed to by the address pointer of the first element), that is, the length of the string.
The str array defined in this code block is not a string array, so the position of'\ 0' is not after the last element, but a random position, and the strlen function calculates the length until it meets'\ 0' and does not stop working and return the result.
Therefore, strlen (str2) returns a random value that does not represent the length of the array.
It is verified by debugging that there is a'\ 0' at the end of the str1 string array and str2 does not.
What is the 2.strcpy function?
Strcpy, the English full name copies strcpy, is also a more common function in the string header file, which is used to copy the contents of one string to another array.
Specific usage
(citing the description in C++ reference again, it is found that the description of this tool website is much clearer than that in Chinese.)
✅ strcpy is used by passing two pointers to it, the first is the address pointer of the first element of the target array, and the latter is the address pointer of the original string array, which copies the original string array, including the trailing flag'\ 0', to the target array. If there are elements in the target array, they are overwritten one by one according to the length of the original string array. (❗ Note: to prevent array overflow, the target array should be longer than the original string array.)
The ⭕ demo code is as follows:
# includeint main () {char str1 [] = "abcdefg"; char str2 [] = "123"; strcpy (str1, str2); / / str2== > str1 printf ("% s ", str1); / / print copy processed str1 return 0;}
Running result:
As you can see, the contents of str2 are perfectly copied into str1.
Through debugging, we can find that the content of str2 covers the first four elements of str1, while the latter elements still exist. However, because the'\ 0' of str2 has also been copied, when printing, because the printf format controller is% s (string type), when the first'\ 0' is encountered, it is considered to be the end of the string flag, ending printing. Therefore, the printed result is the string "123".
What is the 3.strcmp function?
Strcmp function, English full name compares string (string comparison), is used to compare two strings, so what is the comparison of strings here? The comparison here is like this: first compare the ASCII code value of the first character of the two strings (here it is regarded as the first pair of characters), (the following comparisons are the comparison of ASCII code values) if the first character of the first string is greater than the first character of the second string, return 1; otherwise, return-1 If two characters are equal, the next pair of characters is compared until a different pair of characters returns the corresponding value (1 or-1). If all characters of the two strings are the same, the number 0 is returned.
Specific usage:
Pass in two pointers that represent the address of the first element of the two string arrays.
The ⭕ demo code is as follows:
# includeint main () {char str1 [20]; char str2 [20]; scanf ("% s% s", str1, str2); int ret=strcmp (str1, str2); switch (ret) {case 0:printf ("str1=str2"); break; case 1:printf ("str1 > str2"); break; case-1:printf ("str1"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.