C and pointer (pointers on C) -- Chapter 9: String, character, and byte (upper)

Source: Internet
Author: User
Tags uppercase letter
Chapter 9 strings, characters, and bytes


This is easily overlooked when you are new to the system. However, strings, as an important data type, are not explicitly described in C. It seems that C ++ defines the string data type, which has brought a lot of benefits.
If you want to use C to play with OJ or use data structures and algorithms, read this chapter carefully, because this is the only standard library that you can use for string processing.


Summary:
To use the string standard library of C, you must declare "string. H ".
Strlen is used to calculate the length of a string. Its return value is an unsigned integer. The following describes possible problems.
The strcpy function assigns a string a value from one position to another.
The strcat function adds one copy of a string to the end of another string.
The strcmp function compares two strings in alphabetical order. The returned value indicates whether the first string is greater than, less than, or equal.
Strncpy/strncat/strncmp corresponds to unrestricted versions, which can perfectly determine whether the operation overflows. Add a length parameter.
The strncpy length specifies how many characters are written to the target character array.
The strncat length specifies the maximum number of values assigned from the source string, ending with '\ 0 '.
The strcmp length specifies the number of comparisons.
Query string:
Strchr is used to locate the first occurrence of a character in a string.
Strrchr is the same as the preceding one, but returns the last occurrence location.
Strpbrk searches for any character in a specified character set in a string (not a substring! Is any character !) The location where the first image appears.
Strstr is the first occurrence of a substring.


I don't want to involve advanced lookup functions here. The functions are too strange.
Strerror uses an error code as its parameter and returns a pointer to a string to describe this error.


Functions for testing and converting characters:
Their significance is that code portability can be greatly improved.
Toupper converts a lowercase letter to an uppercase letter.
Tolower converts an uppercase letter to a lowercase letter.
Iscntrl checks whether it is a control character.
... A lot of such... They return an integer value, indicating true or false.


Memory operations:
The memxxx function provides string-like operations, but does not stop operations in case of NUL. It also includes a length parameter.
Memmove/memcpy values specify the number of bytes in length
Memcmp comparison
Memchr looks for a specific value
Memset initializes a sequence into a specific value.


Warning:
1. Use a signed expression to use the strlen function.
Because strlen returns size_t as defined, this is an unsigned number, so:
If (strlen (x)> = strlen (y ))...
If (strlen (x)-strlen (y)> = 0 )...
The two may be different. The former will work as expected, but the latter cannot express a negative value.
It is best to forcibly convert the strlen return value to int.
2. Mix the signed and unsigned numbers in the expression.
Same as above.
3. Use the strcpy function to assign a long string to a short array, resulting in overflow.
If it is too long, strcpy will not consider overflow, and its operation result will overwrite the memory space.
4. Use the strcat function to add a string to an array, resulting in array overflow.
Same as above.
5. Compare the return value 1 and-1 of the strcmp function, or use it as a Boolean value.
This is a bad style. The best way is to compare it with 0.
If (strcmp (a, B), it is likely that the result of A and B is equal, but returns a false result.
6. Use a sequence of characters not ending with NUL characters as the transfer value of the above functions.
For example, strcmp and strlen functions are all looking for NUL as the condition for termination. If there is no NUL, the returned value will be uncertain.
7. Use the strncpy function to generate a string that is not easy to end with NUL bytes.
After the strncpy function is used, if the length of the copy exceeds the remaining length, although it does not overflow, its results will not end with NUL.
8. Mix strncpy and strxxx functions.
Warning.


Programming tips:
1. Do not try to write functions with the same functions to replace library functions.
Library functions are sometimes written in assembly to achieve maximum speed. If you want to improve the efficiency, check your other code.
2. Using character classification and conversion functions can improve the portability of functions.







C and pointer (pointers on C) -- Chapter 9: String, character, and byte (upper)

Related Article

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.