Character function and string function (C language) (1,2,3)

Character function and string function (C language) (1,2,3)
Note: The
characteristics of the string in the C language:
Character function and string function (C language) (1,2,3)
Function introduction:
Character function and string function (C language) (1,2,3)
1. The length of the strlen string is
calculated. The length of the string is counted
. The number of characters before the string'\0'. Key points
Character function and string function (C language) (1,2,3)
for the use of the strlen function
:
1. The actual parameters of the function It is the address
2. Count the number of characters before'\0','\0' is not counted as the string content, not counted
3. The return value of the function is an unsigned number
Character function and string function (C language) (1,2,3)
"" Double quotes quoted string content
is added by default \0' end sign
and single quotation mark quotes a single character, no end sign'\0'
When using'' single quotation mark, the strlen function will always search backwards, until it finds'\0', count the characters before'\0' One belongs to the
Character function and string function (C language) (1,2,3)
classic question type: (be careful to be pitted) The
strlen function returns an unsigned number
3-6=-3
. The complement of a negative number is regarded as an unsigned number, which will be particularly large
Character function and string function (C language) (1,2,3)
. The simulation implementation of the strlen function is
because of the character The address
can be modified to const char* str if it is not changed
1. Create a temporary variable counter
Character function and string function (C language) (1,2,3)
2. Function recursion (cannot create a temporary variable, find the length of the string)
Character function and string function (C language) (1,2,3)
3. Pointer
Character function and string function (C language) (1,2,3)
2. The strcpy string copy function copies
a string to another The key points
Character function and string function (C language) (1,2,3)
of the use of the strcpy function in a string
:
1. The destination of arr1, the source of arr2 means copying the contents of arr2 to arr1
2.'\0' will also copy
3. h is replaced by w and e is replaced by o.......
Character function and string function (C language) (1,2,3)
*** Improved the
pointer
Character function and string function (C language) (1,2,3)
code of the analog implementation of strcpy function :
1.1 generation and
Character function and string function (C language) (1,2,3)
2.2 generation
followed by ++,
found in combination with * : first two characters Assignment, the address +1 after the assignment, jump to the next character, and act as the judgment condition according to the result of the assignment. If the assignment is 0, the destination will become 0. If the loop condition is not met, it will jump out of the loop.
{;}
returns to the destination as an empty statement The starting address of the space is
Character function and string function (C language) (1,2,3)
recommended for 2 generations.
Note:
1. The original string must contain'\0', otherwise it will be accessed out of bounds and cause errors
Character function and string function (C language) (1,2,3)
. 2. The target space is large enough to save my string, otherwise it will Out of bounds
Character function and string function (C language) (1,2,3)
3. The target space must be modifiable, not a constant string, and the constant string cannot be modified
Character function and string function (C language) (1,2,3)
3. The strcat string is appended
to a string and a string is appended to
Character function and string function (C language) (1,2,3)
the use of the strcat function.
The content of arr1 is appended to arr2.
Character function and string function (C language) (1,2,3)
Points of use:
1 The target space is large enough to store my string, otherwise it will be out of bounds
Character function and string function (C language) (1,2,3)
2. Both strings must contain'\0' The
destination string must contain'\0', indicating that I am from'\0' 'At the beginning of appending, the
source string must contain'\0', which means that I will append
'\0' to the destination at the end of the appending.
Character function and string function (C language) (1,2,3)
3. I can’t append a string to myself, and the program will crash.
Character function and string function (C language) (1,2,3)
Why can’t I do it myself ? Add to yourself?
The content is modified, the'\0' at the source is overwritten, there is no '
Code to achieve strcat function:
Character function and string function (C language) (1,2,3)
code optimization:
Character function and string function (C language) (1,2,3)

Guess you like

Origin blog.51cto.com/15083388/2644421