C, C + + character manipulation which comes

Source: Internet
Author: User
Tags alphabetic character first string strcmp uppercase character

1) String manipulation
strcpy (P, p1) copy string
strncpy (P, p1, N) copies the specified length string
Strcat (P, p1) Append string
Strncat (P, p1, N) append a specified length string
Strlen (p) Take string length
strcmp (P, p1) compare strings
STRCASECMP ignoring case comparison strings
STRNCMP (P, p1, N) compares a specified length string
STRCHR (P, c) to find the specified character in a string
STRRCHR (P, c) reverse lookup in string
Strstr (P, p1) Find string
STRPBRK (P, p1) finds any element of the collection in the current string as a collection of all the characters of the target string
STRSPN (P, p1) takes all the characters of the target string as a collection, and finds the offset of any element that does not belong to the collection in the current string
STRCSPN (P, p1) takes all the characters of the target string as a collection, and finds the offset of any element belonging to the collection in the current string
* String handler with the specified length fills the 0 end character after the processed string

2) Conversion of string to numeric type
Strtod (P, ppend) converts a double type value from string p and stores subsequent string pointers to the char* type store that ppend points to.
Strtol (P, ppend, base) converts a long type integer value from the string p, base explicitly sets the integer binary of the conversion, set to 0 to determine the input, 0x, 0X prefix to be interpreted according to a specific format, and the 0 prefix to interpret as an integer in octal format Type
Atoi (p) string conversion to int integer
Atof (p) string conversion to double character points
Atol (p) string conversion to Long integer

3) Character check
Isalpha () Check if it is an alphabetic character
Isupper () Check if it is an uppercase character
Islower () Check if lowercase alphabetic characters
IsDigit () Check if it is a number
Isxdigit () Check if valid characters are represented as hexadecimal digits
Isspace () checks if the character is a space type
Iscntrl () Check if the control character
ISPUNCT () Check if it is a punctuation mark
Isalnum () Check for letters and numbers
Isprint () Check if it is a printable character
Isgraph () Check if it is a graphic character, equivalent to Isalnum () | Ispunct ()

4) Function prototypes
Prototype: strcpy (char destination[], const char source[]);

Function: Copy the string source to the string destination
Routines:
#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str1[10] = {"Tsinghuaok"};
Char str2[10] = {"Computer"};
cout <<strcpy (STR1,STR2) <<endl;
}

The operating result is: computer
The second string will overwrite all the contents of the first string!
Note: When you define an array, the string length of the character array 1 must be greater than or equal to the string length of String 2. You cannot assign a string constant or an array of characters directly to an array of characters using an assignment statement. All string handler functions are included in the header file string.h.


strncpy (char destination[], const char source[], int numchars);

strncpy: Copies the first numchars characters in the string source to the string destination.
Example of application of strncpy function
Prototype: strncpy (char destination[], const char source[], int numchars);
Function: Copies the first numchars characters in the string source into the string destination
Routines:

#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str1[10] = {"Tsinghua"};
Char str2[10] = {"Computer"};
cout <<strncpy (str1,str2,3) <<endl;
}

Running Result: Comnghua
Note: The first numchars characters in the string source will overwrite the first numchars characters in the string destination!

Prototype: strcat (char target[], const char source[]);
Function: string source to the back of the string target
Routines:
#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str1[] = {"Tsinghua"};
Char str2[] = {"Computer"};
cout <<strcpy (STR1,STR2) <<endl;
}

Running Result: Tsinghua computer

Note: You should consider the length of the character array 2 when defining the length of the character array 1, because the length of the new string is the sum of two string lengths after the connection. After the string is concatenated, the end character of the string 1 is automatically removed and a trailing character after the new string is retained at the end of the end string.

Prototype: Strncat (char target[], const char source[], int numchars);
Function: NumChars The first character of the string source to the back of the string target
Routines:

#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str1[] = {"Tsinghua"};
Char str2[] = {"Computer"};
cout <<strncat (str1,str2,3) <<endl;
}

Run Result: Tsinghua Com


Prototype: int strcmp (const char firststring[], const char secondstring[]);
Function: Compare two strings of firststring and secondstring
Routines:

#include <iostream.h>
#include <string.h>
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" <<endl;
Else
cout << "Buffer 2 is less than buffer 1" <<endl;
ptr = strcmp (BUF2,BUF3);
if (ptr > 0)
cout << "Buffer 2 is greater than buffer 3" <<endl;
Else
cout << "Buffer 2 is less than buffer 3" <<endl;
}

The operation results are: 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 of strings
Routines:

#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str[100];
cout << "Please enter a string:";
CIN >>str;
cout << "The length of the string is:" <<strlen (str) << "x" <<endl;
}

Run result the length of the string is x (x for the total number of characters you enter)

Note: The function of the Strlen function is to calculate the actual length of the string, not including '/'. 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);
Dest the preceding count characters to character C. Returns the value of the dest.

void *memmove (void *dest, const void *SRC, size_t count);
Copies the count byte characters from SRC to dest.  If SRC and dest overlap, the function is processed automatically. Returns the value of the dest.

void *memcpy (void *dest, const void *SRC, size_t count);
Copies the count byte characters from SRC to dest.  Just as with the Memmove function, there is no way to handle the overlap of src and dest. Returns the value of the dest.

void *memchr (const void *buf, int c, size_t count);
Finds the first occurrence of the character C in the count byte in front of BUF. The character C was found or the count byte has been searched, and the lookup stops. Successful operation returns the position pointer for the first occurrence of C in buf, otherwise returns NULL.

void *_memccpy (void *dest, const void *SRC, int c, size_t count);
Copies 0 or more bytes of characters from Src to dest. When character c is copied or count characters are copied, replication stops.

If the character c is copied, the function returns a pointer to the character position immediately following the character. Otherwise, NULL is returned.

int memcmp (const void *BUF1, const void *BUF2, size_t count);
Compare BUF1 and Buf2 in front of Count byte size.
The return value is < 0, indicating that BUF1 is less than buf2;
The return value is 0, indicating that buf1 equals buf2;
The return value of > 0 indicates that BUF1 is greater than buf2.

int memicmp (const void *BUF1, const void *BUF2, size_t count);

Compare BUF1 and Buf2 in front of Count bytes. Unlike memcmp, it is not case-sensitive.

The return value is ibid.

Char *strrev (char *string);
Reverses the order of characters in a string of strings.  The null terminator position does not change. Returns a pointer to the adjusted string.

Char *_strupr (char *string);
Replaces all lowercase letters in a string with the corresponding uppercase letters, and the other characters remain unchanged. Returns a pointer to the adjusted string.

Char *_strlwr (char *string);
Replaces all uppercase letters in a string with the corresponding lowercase letters, and the other characters remain unchanged. Returns a pointer to the adjusted string.

Char *strchr (const char *string, int c);
Finds the first occurrence in a string string, and the null terminator is also included in the lookup. Returns a pointer to the position where the character C first appears in a string, or null if it is not found.

Char *strrchr (const char *string, int c);
Finds the last occurrence of a character, C, in a string, that is, an inverse search for a string that contains a null terminator.
Returns a pointer to the last occurrence of the character C in a string, or null if not found.

Char *strstr (const char *string, const char *strsearch);
Finds the Strsearch substring in a string of strings. Returns a pointer to the first occurrence of a substring of strsearch in a string. If no substring strsearch is found, NULL is returned. If the substring strsearch is an empty string, the function returns a string value.

Char *strdup (const char *strsource);
The function runs itself by invoking the malloc function to allocate storage space for the copied strsource string and then copying the strsource into the allocated space. Be careful to release this allocated space in a timely manner.
Returns a pointer to the space allocated for the copied string; If the allocation space fails, a null value is returned.

Char *strcat (char *strdestination, const char *strsource);
Add the source string strsource to the target string strdestination, followed by a null terminator after the resulting new string. The character of the source string strsource overwrites the Terminator null after the target string strdestination. There is no overflow check during the copy or add of the string, so make sure the target string space is large enough.  Cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strncat (char *strdestination, const char *strsource, size_t count);
Adds count characters from the beginning of the source string strsource to the target string strdest. The character of the source string strsource overwrites the Terminator null after the target string strdestination. If count is greater than the source string length, the count value is replaced with the length value of the source string. The resulting new string is automatically appended with a null terminator. As with the STRCAT function, this function cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strcpy (char *strdestination, const char *strsource);
Copies the source string strsource to the location specified by the target string strdestination, including the null terminator. Cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strncpy (char *strdestination, const char *strsource, size_t count);
Copies the count characters from the beginning of the source string strsource to the location specified by the target string strdestination. If the count value is less than or equal to the length of the strsource string and the null terminator is not automatically added in the target string, and count is greater than the length of the strsource string, the strsource is padded with a null terminator to fill the count characters and copied to the target string. Cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strset (char *string, int c);
Sets all characters of string string to character C and encounters a null terminator stop. The function returns a string pointer after the content is adjusted.

Char *strnset (char *string, int c, size_t count);
The string string starts with count characters to character C, and if the count value is greater than the length of a string string, the count value is replaced with the length of string. The function returns a string pointer after the content is adjusted.

size_t strspn (const char *string, const char *strcharset);
Finds the ordinal number of the first occurrence of any character (except the string terminator null) that is not contained in the strCharSet string in the string string. Returns an integer value that specifies the length of a substring that consists of all the characters in characters in a string. If the string starts with a character that is not contained in strCharSet, the function returns a value of 0.

size_t strcspn (const char *string, const char *strcharset);
Finds the ordinal number of the first occurrence of any character in the strCharSet string in a string string, containing the string terminator null.
Returns an integer value that specifies the length of a substring that consists of all characters in a string that are not characters. If the string starts with a character contained in strCharSet, the function returns a value of 0.

Char *strspnp (const char *string, const char *strcharset);
Finds any position pointer that is not included in the strCharSet string (except for the string terminator null) in the string string for the first occurrence. Returns a pointer to the position of the character in the non-strcharset in the first occurrence of the string.

Char *strpbrk (const char *string, const char *strcharset);
Finds the position of any character in the strCharSet string for the first time in a string string and does not contain a string terminator null.
Returns a pointer to the position where any character in the strCharSet first appears in a string. If two string arguments do not contain the same character, a null value is returned.

int strcmp (const char *string1, const char *string2);
Compares string string1 and string2 size.
The return value is < 0, indicating that string1 is less than string2;
The return value is 0, indicating that string1 equals string2;
The return value of > 0 indicates that string1 is greater than string2.

int stricmp (const char *string1, const char *string2);
Compare string string1 and string2 sizes, unlike strcmp, which compare their lowercase versions. The return value is the same as strcmp.

int Strcmpi (const char *string1, const char *string2);
is equivalent to the STRICMP function, but provides a backward-compatible version.

int strncmp (const char *string1, const char *string2, size_t count);
Compares the string string1 and string2 size, comparing only the preceding count characters. During the comparison, the length of any one string is less than count, and count is replaced by the length of the shorter string. At this point, if the characters in front of the two strings are equal, the shorter string is smaller.
A return value of < 0, indicating that the substring of the string1 is less than the substring of string2;
A return value of 0 indicates that the substring of string1 is equal to the substring of string2;
The return value, > 0, indicates that the substring of the string1 is greater than the string2 string.

int strnicmp (const char *string1, const char *string2, size_t count);
Compares the string string1 and string2 size, comparing only the preceding count characters.  Unlike strncmp, they are compared in lowercase versions of their letters. The return value is the same as strncmp.

Char *strtok (char *strtoken, const char *strdelimit);
Finds the next tag in the Strtoken string, and the Strdelimit character set specifies the delimiter that may be encountered in the current lookup call. Returns a pointer to the next tag found in Strtoken. If the token is not found, a null value is returned. Each call modifies the strtoken content, replacing each delimiter encountered with a null character.

C + + concept string manipulation

First, char_traits character feature class
1) Meaning: Wrapping a generic behavior interface for a particular string of elements so that the container implements specific behavior based on the feature information when implemented
2) defines a generic type name

typedef _elem CHAR_TYPE;
typedef int INT_TYPE;
typedef streampos POS_TYPE;
typedef streamoff OFF_TYPE;
typedef mbstate_t STATE_TYPE;

Where Int_type represents the integer representation of a character element when it is converted to a specific encoding, Pos_type, off_type as a string index and a string element offset type, similar to the pointer in a container iteration, iteration type and pointer, the offset type of the iterator. The last State_type is used to store stream state, such as error, format control, and so on.

3) defines the wrapper interface for character/string manipulation so that the invocation of the generic algorithm

Assign (A, b) defines the process of assigned the value of B to a character, implementing A.operator = Behavior
EQ (A, B) defines an equality relationship between a character and b character, and implements the behavior of a.operator = =
Lt (A, B) defines a less than B relationship to achieve A.operator < behavior
Compare (A_ptr, b_ptr, CNT) defines a comparison of two sets of strings, returns an int type, implements a behavior similar to MEMCMP
Length (PTR) defines the length of the string to implement similar strlen behavior
Copy (A_ptr, B_PTR, CNT) defines the replication of two sets of strings, implementing a behavior similar to memcpy
Move (A_ptr, b_ptr, CNT) defines a non-overlapping copy of two sets of strings, implementing a behavior similar to Memmove
Assign (PTR, CNT, CH) defines the process of filling a string, implementing a behavior similar to memset
To_int_type (CH) defines the conversion process for Char_type to Int_type integral type
To_char_type (n) defines the conversion process for Int_type to Char_type character types
Eq_int_type (A, B) defines the equality relationship of two and the int_type corresponding to the current Char_type type
EOF () defines the end-of-string character, using an integer representation
Not_eof (n) defines a non-string end character, which returns 1 if the end character is entered, and the other input returns the original value, which always does not return EOF ()

4) The Int_type type should be an integer encoding of the current character type

Second, std::string is not a sequence container , there is no front () and back () interface for removing the front and end elements, using std::string::operator [] and passing streampos type to obtain specific elements, such as St D::string::size ()-1 Gets the last character as an index


Third, the initialization of basic_string support
1) Default initialization
2) Dispenser
3) Copy Construction
4) local copy [_roff, _roff + _count)
5) local copy + dispenser
6) C string [_ptr, <null>)
7) C string + _count [_ptr, _ptr + _count)
8) C string + allocator
9) C string + _count + allocator [_ptr, _ptr + _count)

Ten) _count * _ch
One) _count * _ch + dispenser
12) iterator [_ITF, _ITL)
13) Iterators + splitters

Character-to-string cannot be initialized, but supports operator = assignment and operator + = cumulative assignment operation.

Four, the interval validity of the string
An index access to a string when it exceeds a valid range of strings, because the string performs subscript access to the built-in character buffer on the implementation, it does not cause an exception, but it will get unpredictable results, which are usually not available.
When you enter a different string as an rvalue, the count for that string is greater than the string size when it is larger than the number of strings.
The actual type of Std::basic_string::size_type is size_t, implemented in Visual C + + 7.1 as Unsigned,std::basic_string::npos is statically set to

(Basic_string<_elem, _traits, _alloc>::size_type) (-1);

When you look for operations such as substrings, the function returns a value of NPOs that represents an illegal index.


V. Comparing 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] compare with S2
4) Compare (off, CNT, S2, OFF2, Cnt2) [off, off + CNT] perform comparison with S2 [OFF2, Cnt2]
5) Compare (off, CNT, p) [off, off + CNT] perform comparison with [P, <null>]
6) Compare (off, CNT, p, cnt2) [off, off + CNT] perform comparison with [P, p + cnt2)

Returns 1, 0, 1 as the comparison result of less than, equal to, and greater than.

  VI, additional data  
1) use operator + = to accept other strings, C-style strings and characters  
2) Use Push_back () to append characters at the tail, And makes the Back_iterator constructed by string can access  
3) Append () additional  
        1, Append (s) Append string  
        2, append (S, off, CNT) append string s [off, off + CNT]  
        3, append (p) Append string [P, <null>]  
         4, append (P, CNT) append string [P, p + cnt)  
        5, Append (n, c) fills n * C&NBSP;
        6, append (INF, INL) append input stream [inf, INL]  

4) Insert () insertion
1. Insert (off, S2) inserts a string
2. Insert (off, S2, OFF2, Cnt2) inserts the string s [off2, OFF2 + cnt2)
3. Insert (off, p) insertion string [P, <null>]
4. Insert (off, p, CNT) insertion string [P, p + CNT]

5. Insert (off, N, c) inserts n * C
6, insert (ITER) element default value padding
7. Insert (ITER, C) inserts a specific element
8, insert (ITER, n, c) inserting n*c
9. Insert (ITER, INF, INL) inserts [INF, INL]

5) operator + (A, B)
The form of operator + is supported in string association operator overloading
1, S + S
2, S + P
3, S + C
4, p + S
5, C + S

Vii. find, replace, and purge
1) Find ()
1, find (c, off) in S [Off, NPOs) look for C
2. Find (P, off, N) finds [P, p + n] in s [Off, NPOs]
3. Find (P, off) finds in S [off, NPOs] [P, <null>]
4. Find (S2, off) finds S2 in S [Off, NPOs]

2) The variant of Find ()  
        1, RFind () has the input form of find (), and   is searched in reverse order;
         2, Find_first_of () has the input form of find (), returns the first matching index  
         3, Find_last_of () has the input form of find (), returns the first matching index  
         4, Find_first_not_of () has the input form of find (), returns the first mismatched index  
        5. Find_last_not_of () has the input form of find () and returns the first mismatched index  

3) Replace replacement ()
1. Replace (off, CNT, S2) replaces s [off, off + cnt] with S2
2. Replace (off, CNT, S2, OFF2, Cnt2) replaces S [off, off + cnt] with S2 [off2, OFF2 + Cnt2]
3. Replace (off, CNT, p) replaces S [off, off + cnt] with [P, <null>]
4. Replace (off, CNT, p, Cnt2) replaces S [off, off + cnt] with [P, p + Cnt2]

5. Replace (off, CNT, N, c) replaces S [off, off + cnt] with C * N

When using iterators:
6. Replace (INF, INL, S2) replaces [INF, INL] with S2
7. Replace (INF, INL, p) replaces [INF, INL] with [P, <null>]
8. Replace (INF, INL, p, CNT) replaces [INF, INL] with [P, p + CNT]
9. Replace (INF, INL, N, c) replaces [INF, INL] with n * C
10. Replace (INF, INL, INF2, InL2) replaces [INF, INL] with [InF2, InL2]

4) Erase () Delete
1, erase (off, CNT) remove S [off, off + cnt] from the string s
2. Erase (ITER) removes *iter from the string s
3. Erase (ITF, ITL) removed from string s [ITF, ITL]

Eight, take out the string
1) Get C-style string
C_STR () returns a C-style string pointer of a constant type, and copy (PTR, cnt, off = 0) copies a string of the specified size to a specific pointer. Data () only invokes the C_STR () implementation in Visual C + + 7.1.
2) Get substring
SUBSTR (off, CNT) obtains a copy of S [off, off + cnt).
3) Copy substring
Copy (P, off, CNT) copies S [off, off + cnt) to p.


Nine, buffer management of strings
The string has a std::vector-like buffer management interface.
Size () Gets the valid element length
Max_size () Gets the effective space that the current memory allocator can allocate
Reserve () reserves space for buffer
Capacity () Gets the capacity of the buffer
Resize () resets the length of the string to which you can specify the initialization value

X. Defining the end of an input iterator
The input stream object is passed to the Istream_iterator to create an input iterator, the input iterator holds a pointer to the input stream object, and the default creation and read stream fails when the pointer is set to 0. And when implementing the operator = = Equality operation between the input iterators, hold an equality comparison of the stream object pointers, so that the input iterator created by default will be used to match the end of the input stream.

* When the input stream fails to read, the user executes if, while the condition is judged, the judgment value is actually converted to the void* type, or according to operator! The return result of the operator, overloading operator void* and operator on the input stream! operator, you can define the behavior of the input stream in a Boolean expression so that, in the case of a stream read failure, the input iterator can be confirmed by a Boolean expression rather than explicitly accessing the fail () member function.

C, C + + character manipulation which comes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.