9.2 String Length
The length of a string is the number of characters it contains. The length is easily computed by counting the characters, as is done in Program 9.1. This implementation illustrates the type of processing used when dealing with strings, but in fact you rarely need to write string functions because the ones provided in the standard library usually will do the job. Should you wish to write a string function, though, be aware that the Standard reserves all function names that begin with str for future expansion of the library.
字符串的长度就是它所包含的字符个数。我们很容易通过对字符进行计数来计算字符串的长度,程序9 .1就是这样做的。这种实现方法说明了处理字符串所使用的处理过程的类型。但是,事实上你极少需要编写字符串函数,因为标准库所提供的函数通常能完成这些任务。不过,如果你还是希望自己编写一个字符串函数,请注意标准保留了所有以str 开头的函数名,用于标准库将来的扩展。
The prototype for the library strlen is:
库函数strlen 的原型如下:
size_t strlen( char const *string );
Note that strlen returns a value of type size_t. This type is defined in the include file stddef.h and is an unsigned integer type. Using unsigned values in expressions can lead to unexpected results. For example, the following two statements appear to be equivalent,
注意strlen 返回一个类型为size t 的值。这个类型是在头文件stddef.h 中定义的,它是一个无符号整数类型。在表达式中使用元符号数可能导致不可预料的结果。例如,下面两个表达式看上去是相等的:
if( strlen( x ) >= strlen( y ) ) ...
if( strlen( x ) – strlen( y ) >= 0 ) ...
but they are not. The first works as you would expect, but the second one is always true. The result of strlen is unsigned, so the expression on the left of the >= is unsigned, and unsigned values can never be negative.
但事实上它们是不相等的。第1 条语句将按照你预想的那样工作,但第2 条语句的结果将永远是真。strlen 的结果是个无符号数,所以操作符>=左边的表达式也将是无符号数,而无符号数绝不可能是负的。
/*
** Compute the length of the string argument.
*/
#include <stddef.h>
size_t
strlen( char const *string )
{
int length;
for( length = 0; *string++ != '\0'; )
length += 1;
return length;
}
Program 9.1 String length
Expressions containing both signed and unsigned values can also produce strange results. The following statements are not equivalent for the same reason as the previous pair.
表达式中如果同时包含了有符号数和无符号数,可能会产生奇怪的结果。和前一对语句一样,下面两条语句并不相等,其原因相同。
if( strlen( x ) >= 10 ) ...
if( strlen( x ) - 10 >= 0 ) ...
Casting the value returned by strlen to an int eliminates this problem.
如果把strlen 的返回值强制转换为int ,就可以消除这个问题。
It is tempting to write your own strlen function, making judicious use of register declarations and clever tricks to make it faster than the library function. It rarely works. The standard library functions are sometimes implemented in assembly language in order to exploit the speed of special string manipulation instructions provided by certain machines. Even on machines without such instructions, your time is better spent concentrating on the algorithms for other parts of your program.Finding a better algorithm is more effective than tuning a bad one, and it is more efficient to reuse existing software than to reinvent it.
你很可能想自行编写strlen 函数,灵活运用register 声明和一些聪明的技巧使它比库函数版本效率更高。这的确是个诱惑,但事实上很少能够如愿。标准库函数有时是用汇编语言实现的,目的就是为了充分利用某些机器所提供的特殊的字符串操纵指令,从而追求最大限度的速度。即使在没有这类特殊指令的机器上,你最好还是把更多的时间花在程序其他部分的算法改进上。寻找一种更好的算法比改良一种差劲的算法是有效率,复用已经存在的软件比重新开发一个效率更高。