Pointers on C——9 Strings, Characters, and Bytes.11

来源:互联网 发布:ubuntu 播放器推荐 编辑:程序博客网 时间:2024/06/06 19:24

​9.10 Summary

A string is a sequence of zero or more characters. The sequence is terminated by a NUL byte. The length of a string is the number of characters it contains. The standard library provides a host of functions that process strings; their prototypes are in the file string.h.

字符串就是零个或多个字符的序列,该序列以一个NUL 字节结尾。字符串的长度就是它所包含的字符的数目。标准库提供了一些函数用于处理字符串,它们的原型位于头文件string.h 中。


The strlen function computes the length of a string. The value that is returned is unsigned, so be careful when using it in expressions. strcpy copies a string from one location to another, and strcat appends a copy of a string to the end of another string. Both of these functions assume that their arguments are valid strings, and the results of both are undefined if the source and destination strings overlap. strcmp performs a lexicographic comparison of two strings. Its return value indicates whether the first string is less than, equal to, or greater than the second string.

strlen 函数用于计算一个字符串的长度,它的返回值是一个无符号整数,所以把它用于表达式时应该小心。strcpy函数把一个字符串从一个位置复制到另一个位置,而strcat 函数把一个字符串的一份拷贝添加到另一个字符串的后面。这两个函数都假定它们的参数是有效的字符串,而且如果源字符串和目标字符串出现重叠,函数的结果是未定义的。strcmp  对两个字符串进行词典序的比较。它的返回值提示第1 个字符串是大于、小于还是等于第2 个字符串。


The length‐restricted functions strncpy, strncat, and strncmp are similar lo their unrestricted counterparts. The difference is that these functions take a length argument. With strncpy, the length specifies how many characters will be written to the destination array. If the source string is longer than the length, the result will not be NUL terminated. The length argument to strncat indicates the maximum number of characters that will be copied from the source string, but the result is always NUL terminated. The length argument to strncmp limits the number of characters that are compared. If the strings do not differ within this length, they are considered equal.

长度受限的函数stmcpy 、stmcat 和strncmp 都类似它们对应的不受限制版本。区别在于这些函数还接受一个长度参数。在stmcpy 中,长度指定了多少个字符将被写入到目标字符数组中。如果源字符串比指定长度更长,结果字符串将不会以NUL 字节结尾。stmcat 函数的长度参数指定从源字符串复制过来的字符的最大数目,但它的结果始终以一个NUL 字节结尾。strcmp 函数的长度参数用于限定字符比较的数目。如果两个字符串在指定的数目里不存在区别,它们便被认为是相等的。


There are several functions that search strings. strchr searches a string for the first occurrence of a character, and strrchr searches a string for the last occurrence of a character. strpbrk searches a string for the first occurrence of any character in a specified set. The strstr function searches a string for the first occurrence of another string.

用于查找字符串的函数有好几个。strchr 函数查找一个字符串中某个字符第1 次出现的位置。strrchr 函数查找一个字符串中某个字符最后一次出现的位置。strpbrk 在一个字符串中查找一个指定字符集中任意字符第1 次出现的位置。strstr 函数在一个字符串中查找另一个字符串第1 次出现的位置。


More advanced string searches are also provided. The strspn function counts the number of characters at the beginning of a string that match any character in a specified set. strcspn counts the number of characters at the beginning of a string that do not match any of the characters in a specified set. The strtok function breaks a string into tokens. Each time it is called, it returns a pointer to the next token in the string. The tokens are separated by one or more characters from a specified set.

标准库还提供了一些更加高级的字符串查找函数。strspn 函数计算一个字符串的起始部分匹配一个指定字符集中任意字符的字符数量。strcsp咀函数计算一个字符串的起始部分不匹配一个指定字符集中任意字符的字符数量。strtok 函数把一个字符串分割成几个标记。每次当它调用时,都返回一个指向字符串中下一个标记位置的指针。这些标记由一个指定字符集的一个或多个字符分隔。


The strerror takes an error code as an argument. It returns a pointer to a string that describes the error.

strerror把一个错误代码作为它的参数。它返回一个指向字符串的指针,该字符串用于描述这个错误。


A variety of functions are supplied for testing and transforming character.Programs that use these functions are more portable than those that perform their own tests or transformations of characters. The touper function converts a lowercase character to uppercase, and the tolower function converts a character the other way.The iscntrl function checks whether its argument is a control character, and isspace tests for white space, isdigit checks for a decimal digit, and isxdigit checks for hexadecimal digits. islower and isupper check for lowercase and uppercase characters, respectively, isalpha looks for alphabetic characters, isalnum looks for alphanumeric characters, and ispunct looks for punctuation characters. Finally,isgraph checks whether its argument has a printable graphic associated with it, and isprint checks for either a graphic character or a space.

标准库还提供了各种用于测试和转换字符的函数。使用这些函数的程序比那些自己执行字符测试和转换的程序更具移植性。touper函数把一个小写字母字符转换为大写形式, tolower 函数则执行相反的任务。iscntrl 函数检查它的参数是不是-个控制字符, isspace 函数测试它的参数是否为空白字符。isdigit 函数用于测试它的参数是否为一个十进制数字字符, isxdigit 函数则检查它的参数是否为一个十六进制数字字符。islower 和isupper 函数分别检查它们的参数是否为大写和小写字母。isalpha 函数检查它的参数是否为字母字符, isalnum 函数检查它的参数是否为字母或数字字符,ispunct 函数检查它的参数是否为标点符号字符。最后, isgraph 函数检查它的参数是否为图形字符,isprint 函数检查它的参数是否为图形字符或空白字符。


The memxxx functions provide capabilities similar to some of the string functions, but process arbitrary byte values, including NUL. Each of the functions takes a length argument memcpy copies bytes from a source to a destination, memmove performs the same function, but its behavior is well‐defined when the source and destination arguments overlap. memcmp compares two sequences of bytes, and memchr searches a sequence of bytes for a specific value. Finally, memset stores a specified value in a sequence.

memxxx 函数提供了类似字符串函数的能力,但它们可以处理包括NUL 字节在内的任意字节。这些函数都接受-个长度参数。memcpy 从源参数向目标参数复制由长度参数指定的字节数。memmove 函数执行相同的功能,但它能够正确处理源参数和目标参数出现重叠的情况。memcmp函数比较两个序列的字节, memchr 函数在一个字节序列中查找一个特定的值。最后, memset 函数把一序列字节初始化为一个特定的值。


上一章 Pointers on C——9  Strings, Characters, and Bytes.10