Use the string.h header file in C to operate C-style strings, which mainly include the following functions: Copy strings: strcpy(), strncpy() Append strings: strcat(), strncat() Compare characters String: strcmp(), strncmp() Calculate string length: strlen() Initialize memory area: memset()
string.h in C Usage in
string.h is a header file containing functions for manipulating C-style strings. To use it in C, you need to include this header file first:
<code class="cpp">#include <cstring></code>
The following commonly used functions are provided in string.h:
Example:
<code class="cpp">#include <cstring> int main() { char str1[] = "Hello"; char str2[10]; strcpy(str2, str1); // 将str1复制到str2 strcat(str2, " World"); // 追加" World"到str2 int len = strlen(str2); // 计算str2的长度 cout << "str2: " << str2 << endl; cout << "Length: " << len << endl; return 0; }</code>
Output:
<code>str2: Hello World Length: 11</code>
The above is the detailed content of How to use string.h in c++. For more information, please follow other related articles on the PHP Chinese website!