Summary of C language string operations (ultra-detailed)
1) string operations
Strcpy (p, p1) copies the string
Strncpy (p, p1, n) copies the specified length string
Strcat (p, p1) Additional string
Strncat (p, p1, n) attaches a specified length string
Strlen (p) returns the string length.
Strcmp (p, p1) Comparison string
Strcasecmp ignores case-insensitive comparison strings
Strncmp (p, p1, n) compares the specified length string
Strchr (p, c) searches for the specified character in the string
Strrchr (p, c) reverse lookup in the string
Strstr (p, p1)
Strpbrk (p, p1) takes all the characters of the target string as a set, and searches for any element of the Set in the current string.
Strspns (p, p1) Take all the characters of the target string as the set, and find the offset of any element that does not belong to the set in the current string.
Strcspn (p, p1) uses all characters of the target string as a set, and searches for the offset of any element in the set in the current string.
* The string handler with the specified length fills in the zero-ending character after the processed string
2) string to numeric type conversion
Strtodd (p, ppend) converts double type values from string p, and stores subsequent string pointers to the char * type stored by ppend.
Strtol (p, ppend, base) converts long integer values from string p. base explicitly sets the converted integer hexadecimal value to 0 and determines the hexadecimal value based on the specific format, 0x and 0X prefixes are interpreted as hexadecimal integers, and 0 prefixes are interpreted as octal integers.
Atoi (p) string to int integer
Atof (p) string to double point
Convert an atol (p) string to a long integer
3) character check
Isalpha () check whether it is a letter character
Isupper () check whether it is an uppercase letter
Islower () check whether it is a lowercase letter
Isdigit () check whether it is a number
Isxdigit () check whether it is a valid character expressed by a hexadecimal number
Isspace () check whether it is a space character
Iscntrl () check whether it is a control character
Ispunct () Check for punctuation
Isalnum () Check for letters and numbers
Isprint () Check for printable characters
Isgraph () checks whether it is a graphical character, which is equivalent to isalnum () | ispunct ()
4) function prototype
Prototype: strcpy (char destination [], const char source []);
Function: copy the string source to the string destination.
Routine:
# Include
# Include
Void main (void)
{
Char str1 [10] = {"tsing1_ OK "};
Char str2 [10] = {"Computer "};
Cout < }
Running result: Computer
The second string will overwrite all the content of the first string!
Note: When defining an array, the string length of character array 1 must be greater than or equal to the string length of character array 2. A String constant or character array cannot be directly assigned to a character array using a value assignment statement. All string processing functions are included in the string. h header file.
Strncpy (char destination [], const char source [], int numchars );
Strncpy: copy the first numchars character in the string source to the string destination.
Strncpy Function Application Example
Prototype: strncpy (char destination [], const char source [], int numchars );
Function: copy the first numchars character in the string source to the string destination.
Routine:
# Include
# Include
Void main (void)
{
Char str1 [10] = {"Tsinghua "};
Char str2 [10] = {"Computer "};
Cout < }
Running result: Comnghua
Note: The first numchars character in the string source will overwrite the first numchars character in the string destination!
Prototype: strcat (char target [], const char source []);
Function: connect the source string to the end of the target string.
Routine:
# Include
# Include
Void main (void)
{
Char str1 [] = {"Tsinghua "};
Char str2 [] = {"Computer "};
Cout < }
Running result: Tsinghua Computer
Note: The length of character array 2 should be considered when defining the length of character array 1, because the length of the new string after connection is the sum of the two strings. After the string is connected, the terminator of string 1 is automatically removed, and a terminator after the new string is retained at the end of the string.
Prototype: strncat (char target [], const char source [], int numchars );
Function: receives the first numchars character of the string source to the end of the string target.
Routine:
# Include
# Include
Void main (void)
{
Char str1 [] = {"Tsinghua "};
Char str2 [] = {"Computer "};
Cout < }
Running result: Tsinghua Com
Prototype: int strcmp (const char firststring [], const char secondstring );
Function: Compares two strings: firststring and secondstring.
Routine:
# Include
# Include
Void main (void)
{
Char buf1 [] = "aaa ";
Char buf2 [] = "bbb ";
Char buf3 [] = "ccc ";
Int ptr;
Ptr = strcmp (buf2, buf1 );
If (ptr> 0)
Cout <"Buffer 2 is greater than buffer 1" < Else
Cout <"Buffer 2 is less than buffer 1" < Ptr = strcmp (buf2, buf3 );
If (ptr> 0)
Cout <"Buffer 2 is greater than buffer 3" < Else
Cout <"Buffer 2 is less than buffer 3" < }
The running result is: Buffer 2 is less than buffer 1.
Buffer 2 is greater than buffer 3
Prototype: strlen (const char string []);
Function: counts the number of characters in a string.
Routine:
# Include
# Include
Void main (void)
{
Char str [100];
Cout <"enter a string :";
Cin> str;
Cout <"The length of the string is:" < }
Running result The length of the string is x (x indicates The total number of characters you entered)
Note: The strlen function is used to calculate the actual length of a string, excluding '\ 0. In addition, the strlen function can also directly test the length of a String constant, such as strlen ("Welcome ").
Void * memset (void * dest, int c, size_t count );
Set the count character before dest to the character c. Return the value of dest.
Void * memmove (void * dest, const void * src, size_t count );
Copy count byte characters from src to dest. If src and dest overlap, the function will automatically process the data and return the value of dest.
Void * memcpy (void * dest, const void * src, size_t count );
Copy count byte characters from src to dest. The function is the same as that of memmove, but it cannot process the overlap between src and dest. The value of dest is returned.
Void * memchr (const void * buf, int c, size_t count );
Search for the first occurrence of character c in the count byte before the buf. if the character c is found or the count byte is searched, the query stops. if the operation succeeds, the position pointer of c appears for the first time in buf. Otherwise, NULL is returned.
Void * _ memccpy (void * dest, const void * src, int c, size_t count );
Copy 0 or multiple bytes from src to dest. When character c is copied or count is copied, the copy is stopped.
If character c is copied, the function returns a pointer next to the character. Otherwise, NULL is returned.
Int memcmp (const void * buf1, const void * buf2, size_t count );
Compare the size of the first count bytes of buf1 and buf2.
Return value <0, indicating that buf1 is smaller than buf2;
The returned value is 0, indicating that buf1 is equal to buf2;
The returned value is greater than 0, indicating that buf1 is greater than buf2.
Int memicmp (const void * buf1, const void * buf2, size_t count );
Compare the count bytes before buf1 and buf2. Unlike memcmp, It is case insensitive.
The return value is the same as the preceding one.
Char * strrev (char * string );
The character order in the string is reversed. The position of the NULL Terminator remains unchanged. The adjusted string pointer is returned.
Char * _ strupr (char * string );
Replace all lower-case letters in string with corresponding upper-case letters, and keep other characters unchanged. Return the pointer of the adjusted string.
Char * _ strlwr (char * string );
Replace all uppercase letters in the string with corresponding lowercase letters, and keep other characters unchanged. Return the pointer of the adjusted string.
Char * strchr (const char * string, int c );
The position where the string appears for the first time. The NULL Terminator is also included in the search. returns a pointer pointing to the first position of character c in string. If no pointer is found, NULL is returned.
Char * strrchr (const char * string, int c );
Searches for the position where character c Last appears in string, that is, searches for string in reverse order, including the NULL Terminator.
Returns a pointer pointing to the last position of character c in the string. If no pointer is found, NULL is returned.
Char * strstr (const char * string, const char * strSearch );
Search for strSearch substrings in string. returns the pointer to the first position of the substring strSearch in the string. if the substring strSearch is not found, NULL is returned. if the substring strSearch is an empty string, the function returns the string value.
Char * strdup (const char * strSource );
When the function is running, it calls the malloc function to allocate storage space for copying strSource strings, and then copies strSource to the allocated space. Note that the allocated space should be released in time.
Returns a pointer to the space allocated for the copied string. If the allocation fails, NULL is returned.
Char * strcat (char * strDestination, const char * strSource );
Add the source string strSource to the target string strDestination, and add the NULL terminator to the new string. the character of the source string strSource overwrites the end character NULL after the target string strDestination. there is no overflow check during string copying or adding, so make sure that the target string space is large enough. the source and target strings cannot be overlapped. the strDestination value returned by the function.
Char * strncat (char * strDestination, const char * strSource, size_t count );
Add the count characters starting from the source string strSource to the target string strDest. the character of the source string strSource overwrites the end character NULL after the target string strDestination. if count is greater than the length of the source string, the count value is replaced with the length value of the source string. the NULL Terminator is automatically added to the new string. like the strcat function, this function cannot process overlapping source and target strings. the strDestination value returned by the function.
Char * strcpy (char * strDestination, const char * strSource );
Copy the location specified by the source string strSource to the target string strDestination, including the NULL Terminator. The source string and target string cannot overlap. The strDestination value is returned by the function.
Char * strncpy (char * strDestination, const char * strSource, size_t count );
Copy the count characters starting from the source string strSource to the position specified by the target string strDestination. if the count value is less than or equal to the length of the strSource string, the NULL Terminator is not automatically added to the target string. When the count value is greater than the length of the strSource string, the strSource is filled with the NULL terminator to count characters and copied to the target string. the source and target strings cannot be overlapped. the strDestination value returned by the function.
Char * strset (char * string, int c );
Set all characters in the string to character c and stop the NULL Terminator. The function returns the adjusted string pointer.
Char * strnset (char * string, int c, size_t count );
Set the start count character of string to c. If the count value is greater than the length of string, replace the count value with the string length. The function returns the adjusted string pointer.
Size_t strspn (const char * string, const char * strCharSet );
Search for the first occurrence sequence number of any character not contained in the strCharSet string (except the string Terminator NULL. returns an integer that specifies the length of a substring consisting of all characters in characters. if string starts with a character not contained in strCharSet, the function returns 0.
Size_t strcspn (const char * string, const char * strCharSet );
Searches for the sequence number of the first occurrence of any character in the strCharSet string, including the string Terminator NULL.
Returns an integer that specifies the length of a substring in a string consisting of all characters other than characters. If the string starts with a character contained in strCharSet, the function returns 0.
Char * strspnp (const char * string, const char * strCharSet );
Find the first position pointer of any character not contained in the strCharSet string (except the string Terminator NULL. returns a pointer to the position where the character in a non-strCharSet appears for the first time in the string.
Char * strpbrk (const char * string, const char * strCharSet );
Searches for the position of any character in the strCharSet string that appears for the first time. It does not contain the string Terminator NULL.
Returns a pointer to the position where any character in strCharSet appears for the first time in the string. If the two string parameters do not contain the same character, the return value is NULL.
Int strcmp (const char * string1, const char * string2 );
Compare the string1 and string2 strings.
Return value <0, indicating that string1 is less than string2;
The return value is 0, indicating that string1 is equal to string2;
The return value is greater than 0, indicating that string1 is greater than string2.
Int stricmp (const char * string1, const char * string2 );
Compare the size of string1 and string2, which are different from those of strcmp. the return value is the same as that of strcmp.
Int strcmpi (const char * string1, const char * string2 );
It is equivalent to the stricmp function and only provides a backward compatible version.
Int strncmp (const char * string1, const char * string2, size_t count );
Compare the string1 and string2 strings. Only the previous count characters are compared. during the comparison, if the length of any string is smaller than count, count is replaced by the length of the shorter string. at this time, if the two strings are equal to the first character, the shorter string is smaller.
Return value <0, indicating the substring of string1 smaller than string2;
The return value is 0, indicating that the substring of string1 is equal to the substring of string2;
The return value is greater than 0, indicating that the substring of string1 is greater than that of string2.
Int strnicmp (const char * string1, const char * string2, size_t count );
Compare the string1 and string2 strings with only the previous count characters. Different from strncmp, compare their lower-case letter versions. The returned values are the same as those of strncmp.
Char * strtok (char * strToken, const char * strDelimit );
Find the next tag in the strToken string. The strDelimit Character Set specifies the delimit that may be encountered in the current query call. returns a pointer pointing to the next tag found in strToken. if no tag is found, NULL is returned. each call modifies the strToken content and replaces each separator encountered with NULL characters.
C ++ concepts string operations
1. char_traits character feature class
1) significance: Wrap the universal behavior interface of a specific string element so that the container can execute a specific behavior based on the feature information.
2) A general type name is defined.
Typedef _ Elem char_type;
Typedef int int_type;
Typedef streampos pos_type;
Typedef streamoff off_type;
Typedef mbstate_t state_type;
Int_type indicates the integer representation when the character element is converted to a specific encoding. pos_type and off_type are used as the types of string index and string element offset respectively, similar to the pointer, iteration type, and pointer in container stack, the offset type of the iterator. The last state_type is used to store the stream status, such as error and format control.
3) defines the character/string operation packaging interface for calling common algorithms
Assign (a, B) defines the process of assigning character B to character a to implement the behavior of a. operator =
Eq (a, B) defines the equal relationship between a and B, and implements a. operator =.
Lt (a, B) defines the relationship between a and B to implement the behavior of a. operator <
Compare (a_ptr, B _ptr, cnt) defines the comparison between two strings and returns the int type to implement behavior similar to memcmp.
Length (ptr) defines the length of a string to implement behavior similar to strlen.
Copy (a_ptr, B _ptr, cnt) defines the replication of two groups of strings to implement behavior similar to memcpy
Move (a_ptr, B _ptr, cnt) defines the non-overlapping copying of the two strings to implement behavior similar to memmove.
Assign (ptr, cnt, ch) defines the process of filling strings to implement behaviors similar to memset.
To_int_type (ch) defines the conversion process from char_type to int_type integer.
To_char_type (n) defines the conversion process from int_type to char_type struct type.
Eq_int_type (a, B) defines two int_type equal relationships with the current char_type
Eof () defines the string Terminator, expressed in integer
Not_eof (n) defines non-string Terminator. If the input Terminator is used, 1 is returned. Other inputs return the original value, that is, no eof () is returned ()
4) int_type should be the integer encoding of the current character type
2. std: string is not a serial container.The front () and back () interfaces are not used to retrieve elements at the front and end, use std: string: operator [] and pass the streampos type to obtain specific elements, such as std :: string: size ()-1 is used as the index to obtain the last character.
Iii. Initialization supported by basic_string
1) default Initialization
2) distributor
3) replication Structure
4) Local replication [_ Roff, _ Roff + _ Count)
5) Local replication + distributor
6) C string [_ Ptr, )
7) C string + _ Count [_ Ptr, _ Ptr + _ Count)
8) C string + distributor
9) C string + _ Count + distributor [_ Ptr, _ Ptr + _ Count)
10) _ Count * _ Ch
11) _ Count * _ Ch + distributor
12) iterator [_ ItF, _ ItL)
13) iterator + distributor
Character to string cannot be initialized, but operator = assignment and operator + = accumulate assignment are supported.
Iv. String interval Validity
When the access to the index of a string exceeds the valid range of the string, it will not cause exceptions because the implementation of the string executes subscript access to the built-in character buffer, however, unpredictable results are usually unavailable.
When another string is input as the right value, the size of the string is calculated when the count of the string is greater than the string size.
The actual type of std: basic_string: size_type is size_t. in Visual C ++ 7.1, it is implemented as unsigned, and std: basic_string: npos is statically set
(Basic_string <_ Elem, _ Traits, _ Alloc >:: size_type) (-1 );
When searching for sub-strings and other operations, the function returns the npos value to indicate that the index is invalid.
5. compare strings
Allowed comparison objects
1) compare (s2) other strings of the same type
2) compare (p) C-style string
3) compare (off, cnt, s2) [off, off + cnt) is compared with s2.
4) compare (off, cnt, s2, off2, cnt2) [off, off + cnt) compare with s2 [off2, cnt2)
5) compare (off, cnt, p) [off, off + cnt) Same as [p, ) Execution comparison
6) compare (off, cnt, p, cnt2) [off, off + cnt) are compared with [p, p + cnt2 ).
Returns-1, 0, and 1 as the comparison result of less than, equal to, and greater.
6. Additional data
1) Use operator ++ = to accept other strings, C-style strings and characters
2) use push_back () to append characters and allow back_iterator constructed by string to access
3) append () appending
1. append (s) append string
2. append (s, off, cnt) append string s [off, off + cnt)
3. append (p) append the string [p, )
4. append (p, cnt) append string [p, p + cnt)
5. append (n, c) is filled with n * c
6. append (InF, InL) the input stream [InF, InL)
4) insert () insert
1. insert (off, s2) inserts a string
2. insert (off, s2, off2, cnt2) to insert the string s [off2, off2 + cnt2)
3. insert (off, p) inserts a string [p, )
4. insert (off, p, cnt) inserts a string [p, p + cnt)
5. insert (off, n, c) insert n * c
6. insert (iter) element default value Filling
7. insert (iter, c) insert specific elements
8. insert (iter, n, c) insert n * c
9. insert (iter, InF, InL) insert [InF, InL)
5) operator + (a, B)
Support operator + in string join operator Overloading
1. s + s
2. s + p
3. s + c
4. p + s
5. c + s
VII. Search, replace, and clear
1) find () Search
1. find (c, off) find c in s [off, npos)
2. find (p, off, n) in s [off, npos) to find [p, p + n)
3. find (p, off) in s [off, npos) to find [p, )
4. find (s2, off) Search for s2 in s [off, npos)
2) find () variants
1. rfind () has the input form of find (), Reverse Lookup
2. find_first_of () has the input form of find (), and returns the first matched index.
3. find_last_of () has the input form of find (), and returns the last matched index.
4. find_first_not_of () has the input form of find (). The first unmatched index is returned.
5. find_last_not_of () has the input form of find (), and returns the first unmatched index to the last.
3) replace ()
1. replace (off, cnt, s2) replace s [off, off + cnt) WITH s2
2. replace (off, cnt, s2, off2, cnt2) replace s [off, off + cnt) WITH s2 [off2, off2 + cnt2)
3. replace (off, cnt, p) replace s [off, off + cnt) with [p, )
4. replace (off, cnt, p, cnt2) s [off, off + cnt) with [p, p + cnt2)
5. replace (off, cnt, n, c) replace s [off, off + cnt) with c * n
When the iterator is used:
6. replace (InF, InL, s2) to replace [InF, InL) WITH s2
7. replace (InF, InL, p) replace [InF, InL) with [p, )
8. replace (InF, InL, p, cnt) with [InF, InL) [p, p + cnt)
9. replace (InF, InL, n, c) to replace [InF, InL) with n * c
10. replace (InF, InL, InF2, InL2) to replace [InF, InL) with [InF2, InL2)
4) Delete erase ()
1. erase (off, cnt) deletes s [off, off + cnt) from string s)
2. erase (iter) removes * iter from string s
3. erase (ItF, ItL) removes [ItF, ItL) from string s)
8. Retrieve strings
1) Obtain a C-style string
C_str () returns the C-style string pointer of the constant type. copy (ptr, cnt, off = 0) copies the string of the specified size to the specified pointer. Data () in Visual C ++ 7.1, only c_str () is called.
2) obtain the substring
Substr (off, cnt) gets a copy of s [off, off + cnt.
3) copy the substring
Copy (p, off, cnt) to copy s [off, off + cnt) to p.
9. String Buffer Management
The string has a buffer management interface similar to std: vector.
Size () to get the valid element length
Max_size () obtains the valid space allocated by the current memory distributor.
Reserve () reserves space for the Buffer Zone
Capacity () gets the buffer capacity
Resize () resets the length of the string and you can specify the initialization value for it.
10. Define the end of the input iterator
The input stream object is passed to istream_iterator to create an input iterator. The input iterator holds the pointer of the input stream object. By default, the pointer is set to 0 when the stream creation and reading fail. In addition, when operator = equals operation is implemented between input iterators, the stream object pointer held is equal and compared. In this way, the input iterator created by default is used to match the end of the input stream.
* When the input stream fails to be read and the user executes the if and while conditions, the judgment value is converted to the void * type first, or according to operator! Returns the operator void * and operator! Operator that defines the behavior of the input stream in a Boolean expression, so that when the stream fails to be read, the input iterator can be confirmed by a Boolean expression, rather than explicitly accessing the fail () member function.