C string, and the character byte (upper)

"  String data type is an important, but the C language does not explicitly string data type because a string in the form of a string constant or stored in the array of characters. String constants will not apply to those procedures modified string All other arrays of characters in the string must be stored in memory or dynamically allocated. library functions described herein processing strings and characters, as well as some related functions having similar capabilities. "

01

String basis

First of all, we understand the basics under the strings. String is a string of zero or more characters, and ending with a bit pattern of all zeros byte NUL. Thus, inter-character string can not contain NUL byte appears. This restriction rarely cause problems, because there is no NUL bytes associated with it printable characters, and this is the reason it was chosen as the terminator. NUL terminator byte string, but is not itself part of the string, the string length does not include NUL bytes.

 

02

Length of the string

The length of the string is the number of characters it contains. It is easy to calculate the length of the string, the following procedures by counting the characters is to do so.

#include <stddef.h>size_t strlen(char const *string){    int length;    for (length = 0; *string++ != '\0'; )        length += 1;    return length;}

This implementation illustrates a processing procedure for the type of string used. However, the fact that you rarely need to write string functions, because the function provided by the standard library is usually able to complete these tasks. However, if you still want to write your own, a string function, please note that standard retains all the function names beginning with str for standard library for future expansion.

03

Unrestricted string functions

The most common string functions are "not restricted", that is to say they are only to determine its length by finding the end of the string parameter NUL bytes. These functions are generally designated block of memory for the result string. When using these functions, the programmer must ensure that the resulting string does not overflow this memory. This section will be discussed in detail.

  1. Copy the string

    Function is used to copy the string is strcpy, its prototype is as follows:

    

char *strcpy(char *dst, char const *src);

This function copies the argument string src to dst parameters, if src and dst overlap present in memory, the result is undefined. Since dst parameters will be modified, it must be a character array or a pointer to dynamically allocated memory pointer, you can not use the string constant.

Previous contents of the destination parameters will be overwritten and lost. Even if a new string is shorter than the original dst memory, due to the end of the new string is NUL bytes, so the last few remaining old string of characters will be effectively removed.

char messgae[] = "Original messgae";...strcpy(message, "Different");

After the successful implementation of strcpy, the array will contain the following elements:

|'D'|'i'|'f'|'e'|'r'|'e'|'n'|'t'|0|'e'|'s'|'s'|'a'|'g'|'e'|0|

NUL byte first few characters behind the string functions can not be accessed, so from a practical point of view, they are already missing the.

caveat:

You must ensure that the target space character array large enough to hold the string you want to copy, if the string than the number leader, the extra characters are still copied, they will overwrite the previous value stored in the memory space of the back of the array, strcpy can not solve this problem, because it can not determine the length of the target character array. E.g:

char message[] = "Original messgae";...strcpy(message, "A different message");

The second string is too long, can not be accommodated in the message array. Therefore, strcpy function will occupy part of the memory space behind the array, just rewrite variables previously stored there. * If you use this function to ensure that the objective function enough to hold the source string, you can avoid a lot of debugging.

2. Connection String

To add a string (connected) to the back of another string, you can use the strcat function. Its prototype is as follows:

char *strcat(char *dst, char const *src);

strcat requires dst parameters previously already contains a string (may be an empty string), it is found at the end of the string, and add a copy of the string src into this position. If the position of src and dst overlap occurs, the result is undefined.

Here is its common usage,

char name[] = "Jim";strcpy(message, "Hello ");strcat(message, name);strcat(message, ", how are you?");

Strcat string argument function each are added to the string message previously stored in the array, the result is the following string:

Hello Jim, how are you?

 

3. The return value of the function

strcpy strcat return and a copy of which the first parameter is a pointer to the target character array, because they are the type of the return value, so you can call these nested functions, as follows:

strcat(strcpy(dst, a), b);

strcpy executed first. It copy the string from a return to dst and dst. This then returns the value of the first argument of the function called strcat, b strcat added back to dst.

This style than the following nested calls for better readability of this style there is no advantage in functionality.

strcpy(dst, a);strcat(dst, b);

In fact, the vast majority of calls to these functions, they just return value is simply ignored.

 

4. String Comparison

Compares two strings involves two-by-character strings corresponding to the comparison, until no match is found so far. First character that does not match the more "small" (in the character set smaller ordinal number) where that character string is considered less than the other string. If a string is in front of another part of the string, then it is considered less than the other string, because it occurs earlier byte NUL terminated. This comparison is called "dictionary comparison" to contain only uppercase letters or lowercase letters containing only characters are compared, the results of this comparison process is always given in alphabetical order and our daily used the same comparison.

Library function strcmp compare two character strings, which has the following prototype:

int strcmp(char const *s1, char const *s2);

If s1 is less than s2, the function returns a value less than zero, if s1 is greater than s2, the function returns a value greater than zero. If the two strings are equal, the function returns to zero.

 

04

Length limited String Functions

The library also contains a number of functions, they are in a different manner strings. These functions accept an explicit length parameter, for comparing the number of characters copied or be defined. These functions provide a convenient mechanism to prevent unexpected characters long overflow from their target array.

Prototypes for these functions are as follows, and they are not limited version of the same, if the source parameters with the parameters of the target overlap, and the results strncat strncpy is undefined.

char *strncpy(char *dst, char const *src, size_t len);char *strncat(char *dst, char const *src, size_t len);int strncmp(char const *s1, char const *s2, size_t len);

And as strcpy, strncpy to copy source character string into the destination array. However, it is always just write len characters to dst. If the value of strlen (src) is less than len, dst on the length len filled with additional NUL character. If the value of strlen (src) is greater than or equal len, then only len characters are copied to the dst. note! Its results will not end with NUL bytes.

caveat:

The results strncpy call may not be a string, because the string must end with NUL bytes. If a place (such as parameter strlen function) is not a need to string a sequence of characters ending NUL bytes, what happens then? strlen function will not know NUL byte is not, so it will continue searching until it finds a far NUL bytes. Perhaps you find hundreds of characters to find, and the return value of the function strlen in essence is a random number. Or, if the function attempts to access system memory allocated to a range outside of this program, the program will crash.

 

Although a limited length strncat also function, but it differs from the present strncpy. It copies len characters from src to back up to the target array. However, strncat always add a NUL string byte after the results, and as it will not be filled with the target array NUL bytes like strncpy. Note that the target array and the original string is not counted in the length strncat. strncat copy up to len characters (plus a NUL-terminated byte) to the target array, it will not remove the pipe string after the original target parameters remaining space enough.

Finally, a strncmp also compare two character strings, but it is up to len bytes compare. If not equal two strings of characters exist before the first len ​​characters, this function as strcmp as the comparison stops and returns the result. If the first len ​​characters of the two strings are equal, the function returns zero.

 

05

String search

There are many standard library functions, they use a variety of methods to find the string. Such a variety of tools to hand code a lot of flexibility.

1. Find a character

Finding a particular character in a string the easiest method is to use and strrchr strchr functions, their prototypes as follows:

char *strchr(char const *str, int ch);char *strrchr(char const *str, int ch);

They noted that the second parameter is an integer value. However, it contains a character value. ch strchr find the location of the character appearing in the 1st string str, the function returns a pointer to the location after finding. If the character string does not exist, the function returns a NULL pointer. strrchr functions and strchr basically the same, except that it returns a pointer to the last position of the character string appears.

2. Look for any number of characters

strpbrk is more common functions. It is not looking for a particular character, but find the location of any set of characters in the first occurrence of the string. Its prototype is as follows:

char *strpbrk(char const *str, char const *group);

This function returns a pointer to the first character position str group matches any character. If no match is found, the function returns a NULL pointer.

The following code:

char string[20] = "Hello there, honey.";char *ans;ans = strpbrk(string, "aeiou");

ans pointing position string + 1, because this position is the position of the second parameter is the first occurrence of the character. As before, this function is case-sensitive.

 

3. Find a substring

To find a substring in the string, you may be used strstr function, which has the following prototype:

char *strstr(char const *s1, char const *s2);

This function looks for the start position s1 s2 entire first occurrence, and returns a pointer to the location of the pointer. If s2 does not fully appear anywhere s1, the function returns a NULL pointer. If the second parameter is a null string, the function returns s1.

The standard library does not exist strrstr and strrpbrk. However, if you need them, they are very easy to achieve.

发布了60 篇原创文章 · 获赞 18 · 访问量 2万+

Guess you like

Origin blog.csdn.net/BadAyase/article/details/101765043