Brief introduction of string using C

0.子串vs公共子序列

1.字符串基础

  • 字符串常量: 以双引号扩起来的字符序列,规定所有的字符串常量都由编译器自动在末尾添加一个空字符
  • 字符数组: 末尾添加了'\0'的字符数组,一般需要显示在末尾添加空字符。
  • 规定C风格的字符串都是以NULL空字符('\0')作为终结符结尾。
  • 所以字符串的长度并不包括NULL字节,如strlen函数

2 标准库中的字符串处理函数

C标准库中头文件<string.h>定义了两组字符串函数(C++中用<string>表示)。

  • 第一组函数的名字以str开头,它主要处理以'\0'结尾的字符串,所以字符串内部不能包含任何'\0'字符。
  • 第二组函数的名字以mem开头,主要考虑非字符串内部含有零值的情形,它能够处理任意的字节序列,操作与字符串函数类似
  • 除了memmove函数外,其他函数都没定义重叠对象间的行为
  • 为了提高程序在不同机器上的移植性,利用typedef定义新类型名,即typedef unsigned int size_t
  • size_t:
  • void *malloc(size_t n);
    void *memcpy(void *s1, void const *s2, size_t n);
    size_t strlen(char const *s);
  • General Information(B.2.12)

    Defined Types
    The requirement that additional types defined in this section end in "_t" was prompted by the problem of name space pollution. It is difficult to define a type (where that type is not one defined by POSIX.1-2008) in one header file and use it in another without adding symbols to the name space of the program. To allow implementors to provide their own types, all conforming applications are required to avoid symbols ending in "_t", which permits the implementor to provide additional types. Because a major use of types is in the definition of structure members, which can (and in many cases must) be added to the structures defined in POSIX.1-2008, the need for additional types is compelling.
    The types, such as ushort and ulong, which are in common usage, are not defined in POSIX.1-2008 (although ushort_t would be permitted as an extension). They can be added to &amp;amp;lt;sys/types.h&amp;amp;gt; using a feature test macro (see POSIX.1 Symbols). A suggested symbol for these is _SYSIII. Similarly, the types like u_short would probably be best controlled by _BSD.

 

Brief introduction of string using C

Brief introduction of string using C

Brief introduction of string using C

Brief introduction of string using C

Explain:while(*pdst++)为假时,*src='\0'赋值给了pdst.

Brief introduction of string using C

Notes:strncpy默认n<=strlen(dst)

Brief introduction of string using C

Brief introduction of string using C

Brief introduction of string using C