{"id":62456,"date":"2026-01-20T00:34:07","date_gmt":"2026-01-19T16:34:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/62456.html"},"modified":"2026-01-20T00:34:07","modified_gmt":"2026-01-19T16:34:07","slug":"c%e8%af%ad%e8%a8%80%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a4%84%e7%90%86%e5%8e%9f%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/62456.html","title":{"rendered":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570"},"content":{"rendered":"<h4>\u4e00\u3001strlen&#xff1a;\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6<\/h4>\n<h5>\u529f\u80fd\u8bf4\u660e<\/h5>\n<p>strlen \u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u6709\u6548\u957f\u5ea6&#xff08;\u4e0d\u5305\u542b\u5b57\u7b26\u4e32\u7ec8\u6b62\u7b26 &#039;\\\\0&#039;&#xff09;&#xff0c;\u533a\u522b\u4e8e sizeof&#xff08;\u8ba1\u7b97\u5185\u5b58\u5927\u5c0f&#xff09;\u3002<\/p>\n<h5>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str[] &#061; &#034;Hello World&#034;;<br \/>\n    \/\/ \u8f93\u51fa 11&#xff08;H\u5230d\u517111\u4e2a\u5b57\u7b26&#xff0c;\u4e0d\u542b&#039;\\\\0&#039;&#xff09;<br \/>\n    printf(&#034;\u5b57\u7b26\u4e32\u957f\u5ea6&#xff1a;%zu\\\\n&#034;, strlen(str));<br \/>\n    \/\/ \u8f93\u51fa 12&#xff08;\u5305\u542b&#039;\\\\0&#039;\u7684\u5185\u5b58\u5927\u5c0f&#xff09;<br \/>\n    printf(&#034;\u5185\u5b58\u5927\u5c0f&#xff1a;%zu\\\\n&#034;, sizeof(str));<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;  \/\/ \u7528\u4e8e\u7a7a\u6307\u9488\u68c0\u67e5<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0strlen&#xff1a;\u6307\u9488\u904d\u5386\u6cd5<br \/>\nsize_t my_strlen(const char *str) {<br \/>\n    assert(str !&#061; NULL);  \/\/ \u9632\u6b62\u4f20\u5165\u7a7a\u6307\u9488\u5bfc\u81f4\u5d29\u6e83<br \/>\n    const char *p &#061; str;  \/\/ \u5b9a\u4e49\u6307\u9488\u6307\u5411\u5b57\u7b26\u4e32\u8d77\u59cb\u4f4d\u7f6e<br \/>\n    \/\/ \u904d\u5386\u76f4\u5230\u9047\u5230&#039;\\\\0&#039;&#xff08;\u5b57\u7b26\u4e32\u7ed3\u675f\u6807\u5fd7&#xff09;<br \/>\n    while (*p !&#061; &#039;\\\\0&#039;) {<br \/>\n        p&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u6307\u9488\u5dee\u503c\u5373\u4e3a\u5b57\u7b26\u4e32\u957f\u5ea6<br \/>\n    return p &#8211; str;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5\u4ee3\u7801<br \/>\nint main() {<br \/>\n    char str1[] &#061; &#034;C Language&#034;;<br \/>\n    char str2[] &#061; &#034;&#034;;  \/\/ \u7a7a\u5b57\u7b26\u4e32<br \/>\n    \/\/ \u8f93\u51fa 10<br \/>\n    printf(&#034;my_strlen(\\\\&#034;C Language\\\\&#034;) &#061; %zu\\\\n&#034;, my_strlen(str1));<br \/>\n    \/\/ \u8f93\u51fa 0<br \/>\n    printf(&#034;my_strlen(\\\\&#034;\\\\&#034;) &#061; %zu\\\\n&#034;, my_strlen(str2));<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u5173\u952e\u903b\u8f91\u89e3\u91ca<\/h5>\n<ul>\n<li>assert(str !&#061; NULL)&#xff1a;\u5fc5\u987b\u68c0\u67e5\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u6307\u9488\u662f\u5426\u4e3a\u7a7a&#xff0c;\u907f\u514d\u8bbf\u95ee\u975e\u6cd5\u5185\u5b58\u3002<\/li>\n<li>\u6838\u5fc3\u903b\u8f91&#xff1a;\u901a\u8fc7\u6307\u9488\u904d\u5386\u5b57\u7b26\u4e32&#xff0c;\u76f4\u5230\u9047\u5230 &#039;\\\\0&#039;&#xff0c;\u6307\u9488\u79fb\u52a8\u7684\u603b\u6b65\u6570\u5c31\u662f\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/li>\n<li>\u8fd4\u56de\u503c\u7c7b\u578b size_t&#xff1a;\u662f C \u8bed\u8a00\u5185\u7f6e\u7684\u65e0\u7b26\u53f7\u6574\u6570\u7c7b\u578b&#xff0c;\u4e13\u95e8\u7528\u4e8e\u8868\u793a\u5185\u5b58\u5927\u5c0f \/ \u957f\u5ea6\u3002<\/li>\n<\/ul>\n<h4>\u4e8c\u3001strcpy&#xff1a;\u5b57\u7b26\u4e32\u62f7\u8d1d<\/h4>\n<h5>\u529f\u80fd\u8bf4\u660e<\/h5>\n<p>strcpy \u5c06\u6e90\u5b57\u7b26\u4e32&#xff08;\u5305\u542b &#039;\\\\0&#039;&#xff09;\u5b8c\u6574\u62f7\u8d1d\u5230\u76ee\u6807\u7f13\u51b2\u533a&#xff0c;\u8981\u6c42\u76ee\u6807\u7f13\u51b2\u533a\u8db3\u591f\u5927&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4\u7f13\u51b2\u533a\u6ea2\u51fa\u3002<\/p>\n<h5>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char dest[20];  \/\/ \u76ee\u6807\u7f13\u51b2\u533a<br \/>\n    char src[] &#061; &#034;Hello C&#034;;<br \/>\n    strcpy(dest, src);  \/\/ \u62f7\u8d1dsrc\u5230dest<br \/>\n    printf(&#034;\u62f7\u8d1d\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest);  \/\/ \u8f93\u51fa Hello C<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0strcpy&#xff1a;\u9010\u5b57\u7b26\u62f7\u8d1d<br \/>\nchar *my_strcpy(char *dest, const char *src) {<br \/>\n    \/\/ \u68c0\u67e5\u76ee\u6807\u548c\u6e90\u6307\u9488\u662f\u5426\u4e3a\u7a7a<br \/>\n    assert(dest !&#061; NULL &amp;&amp; src !&#061; NULL);<br \/>\n    char *p &#061; dest;  \/\/ \u4fdd\u5b58\u76ee\u6807\u7f13\u51b2\u533a\u8d77\u59cb\u5730\u5740&#xff08;\u7528\u4e8e\u8fd4\u56de&#xff09;<br \/>\n    \/\/ \u9010\u5b57\u7b26\u62f7\u8d1d&#xff0c;\u76f4\u5230src\u7684&#039;\\\\0&#039;&#xff08;*src\u4e3a&#039;\\\\0&#039;\u65f6&#xff0c;\u8d4b\u503c\u540e\u5faa\u73af\u7ec8\u6b62&#xff09;<br \/>\n    while ((*p&#043;&#043; &#061; *src&#043;&#043;) !&#061; &#039;\\\\0&#039;);<br \/>\n    return dest;  \/\/ \u8fd4\u56de\u76ee\u6807\u7f13\u51b2\u533a\u5730\u5740&#xff0c;\u652f\u6301\u94fe\u5f0f\u8c03\u7528<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5\u4ee3\u7801<br \/>\nint main() {<br \/>\n    char dest[20];<br \/>\n    char src[] &#061; &#034;String Copy&#034;;<br \/>\n    my_strcpy(dest, src);<br \/>\n    printf(&#034;my_strcpy\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest);  \/\/ \u8f93\u51fa String Copy<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u5173\u952e\u903b\u8f91\u89e3\u91ca<\/h5>\n<ul>\n<li>(*p&#043;&#043; &#061; *src&#043;&#043;) !&#061; &#039;\\\\0&#039;&#xff1a;\u8fd9\u662f\u6838\u5fc3\u8bed\u53e5&#xff0c;\u5b8c\u6210 3 \u4ef6\u4e8b&#xff1a;\n<li>\u5c06 src \u6307\u5411\u7684\u5b57\u7b26\u8d4b\u503c\u7ed9 dest \u6307\u5411\u7684\u4f4d\u7f6e&#xff1b;<\/li>\n<li>src \u548c dest \u6307\u9488\u5404\u5411\u540e\u79fb\u52a8\u4e00\u4f4d&#xff1b;<\/li>\n<li>\u5224\u65ad\u8d4b\u503c\u7684\u5b57\u7b26\u662f\u5426\u4e3a &#039;\\\\0&#039;&#xff0c;\u82e5\u662f\u5219\u7ec8\u6b62\u5faa\u73af&#xff08;\u4fdd\u8bc1\u62f7\u8d1d\u5b8c\u6574\u5b57\u7b26\u4e32&#xff09;\u3002<\/li>\n<\/li>\n<li>\u8fd4\u56de\u76ee\u6807\u5730\u5740&#xff1a;\u652f\u6301 strcpy(strcpy(a, b), c) \u8fd9\u7c7b\u94fe\u5f0f\u8c03\u7528\u3002<\/li>\n<\/ul>\n<h4>\u4e09\u3001strcat&#xff1a;\u5b57\u7b26\u4e32\u62fc\u63a5<\/h4>\n<h5>\u529f\u80fd\u8bf4\u660e<\/h5>\n<p>strcat \u5c06\u6e90\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u672b\u5c3e&#xff08;\u8986\u76d6\u76ee\u6807\u539f\u6709\u7684 &#039;\\\\0&#039;&#xff0c;\u6700\u7ec8\u6dfb\u52a0\u65b0\u7684 &#039;\\\\0&#039;&#xff09;\u3002<\/p>\n<h5>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char dest[30] &#061; &#034;Hello, &#034;;  \/\/ \u76ee\u6807\u5b57\u7b26\u4e32<br \/>\n    char src[] &#061; &#034;World!&#034;;<br \/>\n    strcat(dest, src);  \/\/ \u62fc\u63a5src\u5230dest\u672b\u5c3e<br \/>\n    printf(&#034;\u62fc\u63a5\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest);  \/\/ \u8f93\u51fa Hello, World!<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0strcat&#xff1a;\u5148\u627e\u76ee\u6807\u672b\u5c3e&#xff0c;\u518d\u62f7\u8d1d\u6e90\u5b57\u7b26\u4e32<br \/>\nchar *my_strcat(char *dest, const char *src) {<br \/>\n    assert(dest !&#061; NULL &amp;&amp; src !&#061; NULL);<br \/>\n    char *p &#061; dest;<br \/>\n    \/\/ \u7b2c\u4e00\u6b65&#xff1a;\u627e\u5230\u76ee\u6807\u5b57\u7b26\u4e32\u7684&#039;\\\\0&#039;\u4f4d\u7f6e<br \/>\n    while (*p !&#061; &#039;\\\\0&#039;) {<br \/>\n        p&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u4ece&#039;\\\\0&#039;\u4f4d\u7f6e\u5f00\u59cb\u62f7\u8d1d\u6e90\u5b57\u7b26\u4e32&#xff08;\u590d\u7528strcpy\u903b\u8f91&#xff09;<br \/>\n    while ((*p&#043;&#043; &#061; *src&#043;&#043;) !&#061; &#039;\\\\0&#039;);<br \/>\n    return dest;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5\u4ee3\u7801<br \/>\nint main() {<br \/>\n    char dest[30] &#061; &#034;I love &#034;;<br \/>\n    char src[] &#061; &#034;C Programming&#034;;<br \/>\n    my_strcat(dest, src);<br \/>\n    printf(&#034;my_strcat\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest);  \/\/ \u8f93\u51fa I love C Programming<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u5173\u952e\u903b\u8f91\u89e3\u91ca<\/h5>\n<ul>\n<li>\u6838\u5fc3\u5206\u4e24\u6b65&#xff1a;\u5148\u5b9a\u4f4d\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u4f4d\u7f6e&#xff08;&#039;\\\\0&#039;&#xff09;&#xff0c;\u518d\u4ece\u8be5\u4f4d\u7f6e\u5f00\u59cb\u62f7\u8d1d\u6e90\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u6ce8\u610f\u4e8b\u9879&#xff1a;\u76ee\u6807\u7f13\u51b2\u533a\u5fc5\u987b\u9884\u7559\u8db3\u591f\u7a7a\u95f4&#xff0c;\u5426\u5219\u4f1a\u6ea2\u51fa&#xff08;\u6bd4\u5982 dest \u6570\u7ec4\u5927\u5c0f\u8981\u80fd\u5bb9\u7eb3\u539f\u6709\u5185\u5bb9 &#043; \u6e90\u5b57\u7b26\u4e32\u5185\u5bb9 &#043;&#039;\\\\0&#039;&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u56db\u3001strcmp&#xff1a;\u5b57\u7b26\u4e32\u6bd4\u8f83<\/h4>\n<h5>\u529f\u80fd\u8bf4\u660e<\/h5>\n<p>strcmp \u6309 ASCII \u7801\u503c\u9010\u5b57\u7b26\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u8fd4\u56de\u503c\u89c4\u5219&#xff1a;<\/p>\n<ul>\n<li>\u8fd4\u56de 0&#xff1a;\u4e24\u4e2a\u5b57\u7b26\u4e32\u5b8c\u5168\u76f8\u7b49&#xff1b;<\/li>\n<li>\u8fd4\u56de &lt;0&#xff1a;\u7b2c\u4e00\u4e2a\u4e0d\u540c\u5b57\u7b26\u4e2d&#xff0c;str1 \u7684\u5b57\u7b26 ASCII \u503c &lt; str2 \u7684&#xff1b;<\/li>\n<li>\u8fd4\u56de &gt;0&#xff1a;\u7b2c\u4e00\u4e2a\u4e0d\u540c\u5b57\u7b26\u4e2d&#xff0c;str1 \u7684\u5b57\u7b26 ASCII \u503c &gt; str2 \u7684\u3002<\/li>\n<\/ul>\n<h5>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str1[] &#061; &#034;Apple&#034;;<br \/>\n    char str2[] &#061; &#034;Banana&#034;;<br \/>\n    char str3[] &#061; &#034;Apple&#034;;<\/p>\n<p>    int ret1 &#061; strcmp(str1, str2);  \/\/ Apple &lt; Banana&#xff0c;\u8fd4\u56de\u8d1f\u6570<br \/>\n    int ret2 &#061; strcmp(str1, str3);  \/\/ \u76f8\u7b49&#xff0c;\u8fd4\u56de0<br \/>\n    int ret3 &#061; strcmp(str2, str1);  \/\/ Banana &gt; Apple&#xff0c;\u8fd4\u56de\u6b63\u6570<\/p>\n<p>    printf(&#034;strcmp(Apple, Banana) &#061; %d\\\\n&#034;, ret1);  \/\/ \u8f93\u51fa -1<br \/>\n    printf(&#034;strcmp(Apple, Apple) &#061; %d\\\\n&#034;, ret2);   \/\/ \u8f93\u51fa 0<br \/>\n    printf(&#034;strcmp(Banana, Apple) &#061; %d\\\\n&#034;, ret3);  \/\/ \u8f93\u51fa 1<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0strcmp&#xff1a;\u9010\u5b57\u7b26\u6bd4\u8f83ASCII\u503c<br \/>\nint my_strcmp(const char *str1, const char *str2) {<br \/>\n    assert(str1 !&#061; NULL &amp;&amp; str2 !&#061; NULL);<br \/>\n    \/\/ \u904d\u5386\u76f4\u5230\u5b57\u7b26\u4e0d\u540c\u6216\u90fd\u5230&#039;\\\\0&#039;<br \/>\n    while (*str1 !&#061; &#039;\\\\0&#039; &amp;&amp; *str2 !&#061; &#039;\\\\0&#039; &amp;&amp; *str1 &#061;&#061; *str2) {<br \/>\n        str1&#043;&#043;;<br \/>\n        str2&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u8fd4\u56de\u7b2c\u4e00\u4e2a\u4e0d\u540c\u5b57\u7b26\u7684ASCII\u5dee\u503c&#xff08;unsigned char\u907f\u514d\u8d1f\u6570\u6ea2\u51fa&#xff09;<br \/>\n    return (unsigned char)*str1 &#8211; (unsigned char)*str2;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5\u4ee3\u7801<br \/>\nint main() {<br \/>\n    char str1[] &#061; &#034;Cat&#034;;<br \/>\n    char str2[] &#061; &#034;Car&#034;;<br \/>\n    char str3[] &#061; &#034;Cat&#034;;<\/p>\n<p>    printf(&#034;my_strcmp(Cat, Car) &#061; %d\\\\n&#034;, my_strcmp(str1, str2));  \/\/ t(116)-r(114)&#061;2<br \/>\n    printf(&#034;my_strcmp(Cat, Cat) &#061; %d\\\\n&#034;, my_strcmp(str1, str3));  \/\/ 0<br \/>\n    printf(&#034;my_strcmp(Car, Cat) &#061; %d\\\\n&#034;, my_strcmp(str2, str1));  \/\/ -2<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u5173\u952e\u903b\u8f91\u89e3\u91ca<\/h5>\n<ul>\n<li>\u5faa\u73af\u7ec8\u6b62\u6761\u4ef6&#xff1a;\u9047\u5230\u4e0d\u540c\u5b57\u7b26&#xff0c;\u6216\u4efb\u4e00\u5b57\u7b26\u4e32\u5230\u672b\u5c3e&#xff08;&#039;\\\\0&#039;&#xff09;\u3002<\/li>\n<li>(unsigned char)*str1 &#8211; (unsigned char)*str2&#xff1a;\u5c06\u5b57\u7b26\u8f6c\u4e3a\u65e0\u7b26\u53f7\u7c7b\u578b&#xff0c;\u907f\u514d ASCII \u503c\u4e3a 0~127 \u4e4b\u5916\u7684\u5b57\u7b26\u5bfc\u81f4\u8d1f\u6570\u8ba1\u7b97\u9519\u8bef\u3002<\/li>\n<\/ul>\n<h4>\u4e94\u3001strchr&#xff1a;\u67e5\u627e\u5b57\u7b26\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/h4>\n<h5>\u529f\u80fd\u8bf4\u660e<\/h5>\n<p>strchr \u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6307\u5b9a\u5b57\u7b26\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u8be5\u4f4d\u7f6e\u7684\u6307\u9488&#xff1b;\u82e5\u627e\u4e0d\u5230&#xff0c;\u8fd4\u56de NULL\u3002<\/p>\n<h5>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str[] &#061; &#034;Hello World&#034;;<br \/>\n    char *p &#061; strchr(str, &#039;o&#039;);  \/\/ \u67e5\u627e\u5b57\u7b26&#039;o&#039;<br \/>\n    if (p !&#061; NULL) {<br \/>\n        \/\/ \u8f93\u51fa \u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e&#xff1a;o World&#xff08;\u4eceo\u5f00\u59cb\u7684\u5b50\u4e32&#xff09;<br \/>\n        printf(&#034;\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e&#xff1a;%s\\\\n&#034;, p);<br \/>\n        \/\/ \u8f93\u51fa \u7d22\u5f15&#xff1a;4&#xff08;o\u5728\u5b57\u7b26\u4e32\u4e2d\u662f\u7b2c5\u4e2a\u5b57\u7b26&#xff0c;\u7d22\u5f15\u4ece0\u5f00\u59cb&#xff09;<br \/>\n        printf(&#034;\u7d22\u5f15&#xff1a;%zu\\\\n&#034;, p &#8211; str);<br \/>\n    } else {<br \/>\n        printf(&#034;\u672a\u627e\u5230\u5b57\u7b26\\\\n&#034;);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0strchr&#xff1a;\u67e5\u627e\u5b57\u7b26\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<br \/>\nchar *my_strchr(const char *str, int ch) {<br \/>\n    assert(str !&#061; NULL);<br \/>\n    \/\/ \u6ce8\u610f&#xff1a;ch\u4f1a\u88ab\u8f6c\u4e3aunsigned char&#xff0c;\u7edf\u4e00\u5b57\u7b26\u7c7b\u578b<br \/>\n    unsigned char target &#061; (unsigned char)ch;<br \/>\n    \/\/ \u904d\u5386\u5b57\u7b26\u4e32<br \/>\n    while (*str !&#061; &#039;\\\\0&#039;) {<br \/>\n        if ((unsigned char)*str &#061;&#061; target) {<br \/>\n            return (char *)str;  \/\/ \u627e\u5230\u5219\u8fd4\u56de\u5f53\u524d\u6307\u9488<br \/>\n        }<br \/>\n        str&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u7279\u6b8a\u60c5\u51b5&#xff1a;\u67e5\u627e\u7684\u662f&#039;\\\\0&#039;&#xff08;\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26&#xff09;<br \/>\n    if (target &#061;&#061; &#039;\\\\0&#039;) {<br \/>\n        return (char *)str;<br \/>\n    }<br \/>\n    return NULL;  \/\/ \u672a\u627e\u5230\u8fd4\u56deNULL<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5\u4ee3\u7801<br \/>\nint main() {<br \/>\n    char str[] &#061; &#034;Programming&#034;;<br \/>\n    char *p1 &#061; my_strchr(str, &#039;g&#039;);<br \/>\n    char *p2 &#061; my_strchr(str, &#039;z&#039;);<\/p>\n<p>    if (p1) {<br \/>\n        printf(&#034;\u627e\u5230&#039;g&#039;&#xff1a;%s&#xff0c;\u7d22\u5f15&#xff1a;%zu\\\\n&#034;, p1, p1 &#8211; str);  \/\/ \u8f93\u51fa \u627e\u5230&#039;g&#039;&#xff1a;gramming&#xff0c;\u7d22\u5f15&#xff1a;3<br \/>\n    }<br \/>\n    if (p2 &#061;&#061; NULL) {<br \/>\n        printf(&#034;\u672a\u627e\u5230&#039;z&#039;\\\\n&#034;);  \/\/ \u8f93\u51fa \u672a\u627e\u5230&#039;z&#039;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>\u5173\u952e\u903b\u8f91\u89e3\u91ca<\/h5>\n<ul>\n<li>int ch \u53c2\u6570&#xff1a;\u6807\u51c6\u5e93\u4e2d strchr \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f int \u7c7b\u578b&#xff08;\u800c\u975e char&#xff09;&#xff0c;\u76ee\u7684\u662f\u652f\u6301\u4f20\u5165 EOF&#xff08;\u503c\u4e3a &#8211; 1&#xff09;&#xff0c;\u56e0\u6b64\u9700\u8981\u8f6c\u4e3a unsigned char \u7edf\u4e00\u5904\u7406\u3002<\/li>\n<li>\u8fd4\u56de\u6307\u9488\u7684\u610f\u4e49&#xff1a;\u8fd4\u56de\u7684\u6307\u9488\u6307\u5411\u5339\u914d\u5b57\u7b26\u7684\u4f4d\u7f6e&#xff0c;\u901a\u8fc7 p &#8211; str \u53ef\u8ba1\u7b97\u51fa\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>\u603b\u7ed3<\/h4>\n<li>\u6838\u5fc3\u57fa\u7840&#xff1a;C \u8bed\u8a00\u5b57\u7b26\u4e32\u4ee5 &#039;\\\\0&#039; \u4e3a\u7ed3\u675f\u6807\u5fd7&#xff0c;\u6240\u6709\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u90fd\u5fc5\u987b\u56f4\u7ed5 &#039;\\\\0&#039; \u8bbe\u8ba1\u903b\u8f91&#xff0c;\u5426\u5219\u4f1a\u51fa\u73b0\u5185\u5b58\u8d8a\u754c\u6216\u7ed3\u679c\u9519\u8bef\u3002<\/li>\n<li>\u5b89\u5168\u7b2c\u4e00&#xff1a;\u624b\u52a8\u5b9e\u73b0\u65f6\u5fc5\u987b\u5148\u68c0\u67e5\u6307\u9488\u662f\u5426\u4e3a NULL&#xff08;\u7528 assert \u6216\u6761\u4ef6\u5224\u65ad&#xff09;&#xff0c;\u907f\u514d\u7a7a\u6307\u9488\u8bbf\u95ee\u5d29\u6e83&#xff1b;\u540c\u65f6\u8981\u4fdd\u8bc1\u76ee\u6807\u7f13\u51b2\u533a\u8db3\u591f\u5927&#xff0c;\u9632\u6b62\u6ea2\u51fa\u3002<\/li>\n<li>\u6307\u9488\u64cd\u4f5c&#xff1a;\u5b57\u7b26\u4e32\u51fd\u6570\u7684\u6838\u5fc3\u662f\u6307\u9488\u904d\u5386&#xff0c;\u7406\u89e3 *p&#043;&#043; \u8fd9\u7c7b\u6307\u9488\u64cd\u4f5c\u7684\u542b\u4e49&#xff0c;\u662f\u638c\u63e1\u5b57\u7b26\u4e32\u5904\u7406\u7684\u5173\u952e\u3002<\/li>\n<p>\u8fd9\u4e9b\u624b\u52a8\u5b9e\u73b0\u7684\u51fd\u6570\u4e0e\u6807\u51c6\u5e93\u903b\u8f91\u4e00\u81f4&#xff08;\u7b80\u5316\u7248&#xff09;&#xff0c;\u4f60\u53ef\u4ee5\u57fa\u4e8e\u8fd9\u4e9b\u4ee3\u7801\u6269\u5c55\u529f\u80fd&#xff08;\u6bd4\u5982\u5b9e\u73b0 strncpy\u3001strncat \u7b49\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b89\u5168\u7248\u672c&#xff09;\u3002<\/p>\n<p>\u6dfb\u52a0strncpy\u3001strncat\u7b49<\/p>\n<p>\u5df2\u5b8c\u6210\u601d\u8003<\/p>\n<p>\u7528\u6237\u73b0\u5728\u8981\u6c42\u6dfb\u52a0 strncpy\u3001strncat \u7b49\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u548c\u5b9e\u4f8b&#xff0c;\u9996\u5148\u6211\u9700\u8981\u5148\u590d\u8ff0\u7528\u6237\u7684\u6838\u5fc3\u9700\u6c42&#xff1a;\u7528\u6237\u5e0c\u671b\u5728\u4e4b\u524d\u8bb2\u89e3\u7684 strlen\u3001strcpy \u7b49\u51fd\u6570\u57fa\u7840\u4e0a&#xff0c;\u8865\u5145 strncpy\u3001strncat \u8fd9\u4e24\u4e2a\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b89\u5168\u7248\u5b57\u7b26\u4e32\u51fd\u6570\u7684\u529f\u80fd\u8bf4\u660e\u3001\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801\u548c\u5173\u952e\u903b\u8f91\u89e3\u91ca&#xff0c;\u5e2e\u52a9\u7406\u89e3\u8fd9\u7c7b\u5b89\u5168\u7248\u51fd\u6570\u7684\u5e95\u5c42\u5b9e\u73b0\u548c\u4f7f\u7528\u8981\u70b9\u3002<\/p>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6309\u7167\u4e4b\u524d\u7684\u7ed3\u6784\u6765\u7ec4\u7ec7\u5185\u5bb9&#xff0c;\u5148\u8bb2 strncpy&#xff0c;\u518d\u8bb2 strncat&#xff0c;\u6bcf\u4e2a\u90e8\u5206\u90fd\u5305\u542b\u529f\u80fd\u8bf4\u660e\u3001\u6807\u51c6\u5e93\u5b9e\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u3001\u5173\u952e\u89e3\u91ca&#xff0c;\u6700\u540e\u8fd8\u8981\u8865\u5145\u603b\u7ed3&#xff0c;\u628a\u65b0\u589e\u7684\u8981\u70b9\u52a0\u8fdb\u53bb\u3002<\/p>\n<p>\u9996\u5148\u5904\u7406 strncpy&#xff1a;<\/p>\n<ul>\n<li>\u529f\u80fd\u8bf4\u660e&#xff1a;\u91cd\u70b9\u5f3a\u8c03\u662f\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b57\u7b26\u4e32\u62f7\u8d1d&#xff0c;\u89e3\u51b3 strcpy \u7684\u7f13\u51b2\u533a\u6ea2\u51fa\u95ee\u9898&#xff0c;\u6ce8\u610f\u5982\u679c\u6e90\u5b57\u7b26\u4e32\u957f\u5ea6\u5c0f\u4e8e n&#xff0c;\u4f1a\u7528 &#039;\\\\0&#039; \u586b\u5145\u5230 n \u4e2a\u5b57\u7b26&#xff1b;\u5982\u679c\u6e90\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e n&#xff0c;\u76ee\u6807\u4e0d\u4f1a\u81ea\u52a8\u52a0 &#039;\\\\0&#039;&#xff0c;\u8fd9\u662f\u5173\u952e\u6613\u9519\u70b9\u3002<\/li>\n<li>\u6807\u51c6\u5e93\u5b9e\u4f8b&#xff1a;\u8981\u5c55\u793a\u4e0d\u540c\u573a\u666f&#xff0c;\u6bd4\u5982\u6e90\u77ed\u4e8e n\u3001\u6e90\u957f\u4e8e n \u7684\u60c5\u51b5&#xff0c;\u7a81\u51fa &#039;\\\\0&#039; \u7684\u95ee\u9898\u3002<\/li>\n<li>\u624b\u52a8\u5b9e\u73b0&#xff1a;\u8981\u4e25\u683c\u9075\u5faa\u6807\u51c6\u903b\u8f91&#xff0c;\u5faa\u73af n \u6b21&#xff0c;\u9010\u5b57\u7b26\u62f7\u8d1d&#xff0c;\u6e90\u7528\u5b8c\u540e\u586b &#039;\\\\0&#039;&#xff0c;\u6ce8\u610f\u8fd4\u56de\u76ee\u6807\u5730\u5740\u3002<\/li>\n<li>\u5173\u952e\u89e3\u91ca&#xff1a;\u91cd\u70b9\u8bb2 n \u7684\u4f5c\u7528&#xff0c;&#039;\\\\0&#039; \u586b\u5145\u89c4\u5219&#xff0c;\u4ee5\u53ca\u6e90\u8fc7\u957f\u65f6\u65e0\u7ec8\u6b62\u7b26\u7684\u5751\u3002<\/li>\n<\/ul>\n<p>\u7136\u540e\u662f strncat&#xff1a;<\/p>\n<ul>\n<li>\u529f\u80fd\u8bf4\u660e&#xff1a;\u5e26\u957f\u5ea6\u9650\u5236\u7684\u62fc\u63a5&#xff0c;\u53ea\u62fc\u63a5\u6700\u591a n \u4e2a\u5b57\u7b26&#xff0c;\u4e14\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u5728\u6700\u540e\u52a0 &#039;\\\\0&#039;&#xff0c;\u6bd4 strncat \u66f4\u5b89\u5168&#xff0c;\u533a\u522b\u4e8e strcat \u7684\u65e0\u9650\u5236\u3002<\/li>\n<li>\u6807\u51c6\u5e93\u5b9e\u4f8b&#xff1a;\u5c55\u793a\u62fc\u63a5 n \u4e2a\u5b57\u7b26\u7684\u573a\u666f&#xff0c;\u5bf9\u6bd4\u6e90\u957f\u5ea6\u5927\u4e8e \/ \u5c0f\u4e8e n \u7684\u60c5\u51b5\u3002<\/li>\n<li>\u624b\u52a8\u5b9e\u73b0&#xff1a;\u5148\u627e\u76ee\u6807\u7684 &#039;\\\\0&#039;&#xff0c;\u7136\u540e\u6700\u591a\u62f7\u8d1d n \u4e2a\u5b57\u7b26&#xff0c;\u6700\u540e\u52a0 &#039;\\\\0&#039;&#xff0c;\u6ce8\u610f\u5faa\u73af\u6761\u4ef6\u662f n&gt;0 \u4e14\u6e90\u6ca1\u5230 &#039;\\\\0&#039;\u3002<\/li>\n<li>\u5173\u952e\u89e3\u91ca&#xff1a;\u5f3a\u8c03\u81ea\u52a8\u52a0 &#039;\\\\0&#039; \u7684\u7279\u6027&#xff0c;\u533a\u522b\u4e8e strncpy&#xff0c;\u4ee5\u53ca n \u662f\u6700\u5927\u62fc\u63a5\u5b57\u7b26\u6570&#xff08;\u4e0d\u542b &#039;\\\\0&#039;&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u8fd8\u8981\u6ce8\u610f\u4fdd\u6301\u548c\u4e4b\u524d\u4e00\u81f4\u7684\u98ce\u683c&#xff0c;\u4ee3\u7801\u6ce8\u91ca\u6e05\u6670&#xff0c;\u5b9e\u4f8b\u6709\u8f93\u51fa\u8bf4\u660e&#xff0c;\u5173\u952e\u903b\u8f91\u7528\u7c97\u4f53\u5f3a\u8c03&#xff0c;\u6700\u540e\u603b\u7ed3\u90e8\u5206\u8981\u8865\u5145\u8fd9\u4e24\u4e2a\u5b89\u5168\u51fd\u6570\u7684\u6838\u5fc3\u8981\u70b9\u3002<\/p>\n<p>\u73b0\u5728\u5f00\u59cb\u7ec4\u7ec7\u5185\u5bb9&#xff0c;\u9996\u5148\u590d\u8ff0\u9700\u6c42&#xff0c;\u7136\u540e\u4f9d\u6b21\u8bb2\u89e3 strncpy\u3001strncat&#xff0c;\u6700\u540e\u603b\u7ed3\u65b0\u589e\u7684\u5173\u952e\u70b9\u3002<\/p>\n<p>\u4f60\u5e0c\u671b\u5728\u4e4b\u524d\u7684\u57fa\u7840\u4e0a&#xff0c;\u8865\u5145 strncpy\u3001strncat \u8fd9\u4e24\u4e2a\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b89\u5168\u7248\u5b57\u7b26\u4e32\u51fd\u6570&#xff08;\u89e3\u51b3 strcpy\/strcat \u7f13\u51b2\u533a\u6ea2\u51fa\u7684\u95ee\u9898&#xff09;&#xff0c;\u4e0b\u9762\u4e3a\u4f60\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u3001\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801\u53ca\u6838\u5fc3\u903b\u8f91\u89e3\u91ca\u3002<\/p>\n<h4>\u516d\u3001strncpy&#xff1a;\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b57\u7b26\u4e32\u62f7\u8d1d<\/h4>\n<h5>\u529f\u80fd\u8bf4\u660e<\/h5>\n<p>strncpy \u662f strcpy \u7684\u5b89\u5168\u7248\u672c&#xff0c;\u6838\u5fc3\u662f\u9650\u5236\u62f7\u8d1d\u7684\u6700\u5927\u5b57\u7b26\u6570&#xff0c;\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa\u3002\u5173\u952e\u89c4\u5219&#xff1a;<\/p>\n<li>\u6700\u591a\u4ece\u6e90\u5b57\u7b26\u4e32 src \u62f7\u8d1d n \u4e2a\u5b57\u7b26\u5230\u76ee\u6807 dest&#xff1b;<\/li>\n<li>\u5982\u679c src \u7684\u957f\u5ea6 &lt; n&#xff1a;\u62f7\u8d1d\u5b8c src \u540e&#xff0c;\u4f1a\u7528 &#039;\\\\0&#039; \u586b\u5145 dest \u76f4\u5230\u603b\u957f\u5ea6\u4e3a n&#xff1b;<\/li>\n<li>\u5982\u679c src \u7684\u957f\u5ea6 \u2265 n&#xff1a;\u4ec5\u62f7\u8d1d\u524d n \u4e2a\u5b57\u7b26&#xff0c;\u76ee\u6807\u5b57\u7b26\u4e32\u4e0d\u4f1a\u81ea\u52a8\u6dfb\u52a0 &#039;\\\\0&#039;&#xff08;\u8fd9\u662f\u6700\u6613\u9519\u70b9&#xff09;&#xff1b;<\/li>\n<li>\u8fd4\u56de\u76ee\u6807\u7f13\u51b2\u533a\u5730\u5740 dest\u3002<\/li>\n<h5>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u573a\u666f1&#xff1a;\u6e90\u5b57\u7b26\u4e32\u957f\u5ea6 &lt; n<br \/>\n    char dest1[10];<br \/>\n    char src1[] &#061; &#034;Hi&#034;;<br \/>\n    strncpy(dest1, src1, 5);  \/\/ \u62f7\u8d1dHi&#xff0c;\u518d\u586b3\u4e2a&#039;\\\\0&#039;\u5230\u603b\u957f\u5ea65<br \/>\n    printf(&#034;\u573a\u666f1&#xff1a;dest1 &#061; %s&#xff08;\u957f\u5ea6&#xff1a;%zu&#xff09;\\\\n&#034;, dest1, strlen(dest1));  \/\/ \u8f93\u51fa Hi&#xff08;\u957f\u5ea62&#xff09;<\/p>\n<p>\u7ed9\u51fa\u5b8c\u6574\u7684c\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570<\/p>\n<p>\u5df2\u5b8c\u6210\u601d\u8003<\/p>\n<p>\u7528\u6237\u73b0\u5728\u9700\u8981\u5b8c\u6574\u7684 C \u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570&#xff0c;\u5305\u62ec\u4e4b\u524d\u7684 strlen\u3001strcpy\u3001strcat\u3001strcmp\u3001strchr&#xff0c;\u52a0\u4e0a strncpy\u3001strncat&#xff0c;\u8fd8\u8981\u8865\u5145 strncmp\u3001strstr\u3001strtok\u3001memset \u8fd9\u4e9b\u5e38\u7528\u7684&#xff0c;\u6bcf\u4e2a\u90fd\u8981\u6709\u529f\u80fd\u8bf4\u660e\u3001\u6807\u51c6\u5e93\u793a\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u3001\u903b\u8f91\u89e3\u91ca&#xff0c;\u6700\u540e\u603b\u7ed3\u3002<\/p>\n<p>\u9996\u5148&#xff0c;\u5148\u68b3\u7406\u7ed3\u6784&#xff0c;\u6bcf\u4e2a\u51fd\u6570\u6309\u7edf\u4e00\u7684\u683c\u5f0f&#xff1a;\u529f\u80fd\u8bf4\u660e\u3001\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801\u3001\u5173\u952e\u903b\u8f91\u89e3\u91ca\u3002<\/p>\n<p>\u9996\u5148&#xff0c;\u5148\u660e\u786e\u7528\u6237\u7684\u6838\u5fc3\u9700\u6c42&#xff1a;\u5b8c\u6574\u7684\u5e38\u7528 C \u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570&#xff0c;\u5305\u542b\u5b89\u5168\u7248&#xff08;\u5e26 n \u7684&#xff09;&#xff0c;\u6bcf\u4e2a\u90fd\u6709\u5b9e\u73b0\u548c\u793a\u4f8b&#xff0c;\u9002\u5408\u65b0\u624b\u7406\u89e3\u3002<\/p>\n<p>\u7136\u540e&#xff0c;\u5f00\u59cb\u7ec4\u7ec7\u5185\u5bb9&#xff1a;<\/p>\n<li>\u5148\u603b\u8ff0&#xff1a;\u4f60\u9700\u8981\u4e00\u5957\u5b8c\u6574\u7684\u3001\u5e38\u7528\u7684 C \u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u5b9e\u73b0&#xff08;\u5305\u542b\u57fa\u7840\u7248\u548c\u5b89\u5168\u7248&#xff09;&#xff0c;\u4e0b\u9762\u6574\u7406\u4e86 10 \u4e2a\u6838\u5fc3\u51fd\u6570&#xff0c;\u6bcf\u4e2a\u90fd\u5305\u542b\u529f\u80fd\u8bf4\u660e\u3001\u6807\u51c6\u5e93\u4f7f\u7528\u793a\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u4ee3\u7801\u548c\u5173\u952e\u903b\u8f91\u89e3\u91ca&#xff0c;\u8986\u76d6\u62f7\u8d1d\u3001\u62fc\u63a5\u3001\u6bd4\u8f83\u3001\u67e5\u627e\u3001\u5206\u5272\u3001\u5185\u5b58\u586b\u5145\u7b49\u6838\u5fc3\u573a\u666f\u3002<\/li>\n<p>\u7136\u540e\u5206\u51fd\u6570\u8bb2\u89e3&#xff1a;<\/p>\n<li>strlen&#xff08;\u57fa\u7840&#xff09;<\/li>\n<li>strcpy&#xff08;\u57fa\u7840&#xff09;<\/li>\n<li>strncpy&#xff08;\u5b89\u5168\u7248\u62f7\u8d1d&#xff09;<\/li>\n<li>strcat&#xff08;\u57fa\u7840\u62fc\u63a5&#xff09;<\/li>\n<li>strncat&#xff08;\u5b89\u5168\u7248\u62fc\u63a5&#xff09;<\/li>\n<li>strcmp&#xff08;\u57fa\u7840\u6bd4\u8f83&#xff09;<\/li>\n<li>strncmp&#xff08;\u5b89\u5168\u7248\u6bd4\u8f83&#xff09;<\/li>\n<li>strchr&#xff08;\u67e5\u627e\u5b57\u7b26&#xff09;<\/li>\n<li>strstr&#xff08;\u67e5\u627e\u5b50\u4e32&#xff09;<\/li>\n<li>strtok&#xff08;\u5b57\u7b26\u4e32\u5206\u5272&#xff09;<\/li>\n<li>memset&#xff08;\u5185\u5b58\u586b\u5145&#xff0c;\u8f85\u52a9\u5b57\u7b26\u4e32\u521d\u59cb\u5316&#xff09;<\/li>\n<p>\u6bcf\u4e2a\u90e8\u5206\u6309\u683c\u5f0f\u6765&#xff0c;\u786e\u4fdd\u4ee3\u7801\u5b8c\u6574\u53ef\u8fd0\u884c&#xff0c;\u89e3\u91ca\u6e05\u6670&#xff0c;\u91cd\u70b9\u7a81\u51fa\u5b89\u5168\u70b9\u548c\u6613\u9519\u70b9\u3002<\/p>\n<p>\u7136\u540e\u6700\u540e\u603b\u7ed3\u5173\u952e\u70b9\u3002<\/p>\n<p>\u73b0\u5728\u5f00\u59cb\u5199\u5177\u4f53\u5185\u5bb9&#xff0c;\u786e\u4fdd\u4ee3\u7801\u6b63\u786e&#xff0c;\u89e3\u91ca\u901a\u4fd7\u6613\u61c2&#xff0c;\u9002\u5408\u65b0\u624b\u3002<\/p>\n<p>\u4f60\u9700\u8981\u4e00\u5957\u5b8c\u6574\u7684\u3001\u5e38\u7528\u7684 C \u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u5b9e\u73b0&#xff08;\u5305\u542b\u57fa\u7840\u7248\u548c\u5b89\u5168\u7248&#xff09;&#xff0c;\u4e0b\u9762\u6574\u7406\u4e86 10 \u4e2a\u6838\u5fc3\u51fd\u6570&#xff0c;\u8986\u76d6\u62f7\u8d1d\u3001\u62fc\u63a5\u3001\u6bd4\u8f83\u3001\u67e5\u627e\u3001\u5206\u5272\u3001\u5185\u5b58\u586b\u5145\u7b49\u573a\u666f&#xff0c;\u6bcf\u4e2a\u51fd\u6570\u90fd\u5305\u542b\u529f\u80fd\u8bf4\u660e\u3001\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b\u3001\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801\u548c\u5173\u952e\u903b\u8f91\u89e3\u91ca&#xff0c;\u4ee3\u7801\u53ef\u76f4\u63a5\u7f16\u8bd1\u8fd0\u884c&#xff0c;\u4e14\u6807\u6ce8\u4e86\u65b0\u624b\u6613\u51fa\u9519\u7684\u7ec6\u8282\u3002<\/p>\n<h4>\u524d\u7f6e\u8bf4\u660e<\/h4>\n<p>\u6240\u6709\u5b57\u7b26\u4e32\u51fd\u6570\u7684\u6838\u5fc3\u524d\u63d0&#xff1a;C \u8bed\u8a00\u5b57\u7b26\u4e32\u4ee5 &#039;\\\\0&#039;&#xff08;\u7a7a\u5b57\u7b26&#xff09;\u4f5c\u4e3a\u7ed3\u675f\u6807\u5fd7&#xff0c;\u624b\u52a8\u5b9e\u73b0\u65f6\u5fc5\u987b\u56f4\u7ed5 &#039;\\\\0&#039; \u5904\u7406&#xff0c;\u540c\u65f6\u91cd\u70b9\u5173\u6ce8\u7a7a\u6307\u9488\u68c0\u67e5\u548c\u7f13\u51b2\u533a\u6ea2\u51fa\u95ee\u9898\u3002<\/p>\n<hr \/>\n<h3>1. strlen&#xff1a;\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u8ba1\u7b97\u5b57\u7b26\u4e32\u6709\u6548\u957f\u5ea6&#xff08;\u4e0d\u542b &#039;\\\\0&#039;&#xff09;&#xff0c;\u8fd4\u56de\u65e0\u7b26\u53f7\u6574\u6570 size_t\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str[] &#061; &#034;Hello C&#034;;<br \/>\n    printf(&#034;\u957f\u5ea6&#xff1a;%zu\\\\n&#034;, strlen(str)); \/\/ \u8f93\u51fa 7&#xff08;H\u5230C\u51717\u4e2a\u5b57\u7b26&#xff09;<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>size_t my_strlen(const char *str) {<br \/>\n    assert(str !&#061; NULL); \/\/ \u7a7a\u6307\u9488\u68c0\u67e5&#xff0c;\u907f\u514d\u5d29\u6e83<br \/>\n    const char *p &#061; str;<br \/>\n    while (*p !&#061; &#039;\\\\0&#039;) { \/\/ \u904d\u5386\u5230&#039;\\\\0&#039;\u7ec8\u6b62<br \/>\n        p&#043;&#043;;<br \/>\n    }<br \/>\n    return p &#8211; str; \/\/ \u6307\u9488\u5dee\u503c &#061; \u5b57\u7b26\u6570<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    printf(&#034;%zu\\\\n&#034;, my_strlen(&#034;Test&#034;)); \/\/ 4<br \/>\n    printf(&#034;%zu\\\\n&#034;, my_strlen(&#034;&#034;));     \/\/ 0<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>assert(str !&#061; NULL) \u662f\u5fc5\u52a0\u7684\u5b89\u5168\u68c0\u67e5&#xff1b;<\/li>\n<li>size_t \u662f\u65e0\u7b26\u53f7\u7c7b\u578b&#xff0c;\u9002\u914d\u5185\u5b58\u957f\u5ea6\u7684\u8868\u793a&#xff08;\u907f\u514d\u8d1f\u6570&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>2. strcpy&#xff1a;\u5b57\u7b26\u4e32\u62f7\u8d1d&#xff08;\u65e0\u957f\u5ea6\u9650\u5236&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u5c06\u6e90\u5b57\u7b26\u4e32&#xff08;\u542b &#039;\\\\0&#039;&#xff09;\u5b8c\u6574\u62f7\u8d1d\u5230\u76ee\u6807\u7f13\u51b2\u533a&#xff0c;\u6613\u6ea2\u51fa&#xff0c;\u4ec5\u9002\u5408\u786e\u5b9a\u7f13\u51b2\u533a\u8db3\u591f\u5927\u7684\u573a\u666f\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char dest[20];<br \/>\n    char src[] &#061; &#034;String Copy&#034;;<br \/>\n    strcpy(dest, src);<br \/>\n    printf(&#034;\u62f7\u8d1d\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest); \/\/ \u8f93\u51fa String Copy<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>char *my_strcpy(char *dest, const char *src) {<br \/>\n    assert(dest !&#061; NULL &amp;&amp; src !&#061; NULL); \/\/ \u53cc\u6307\u9488\u975e\u7a7a\u68c0\u67e5<br \/>\n    char *p &#061; dest; \/\/ \u4fdd\u5b58\u76ee\u6807\u8d77\u59cb\u5730\u5740&#xff08;\u7528\u4e8e\u8fd4\u56de&#xff09;<br \/>\n    while ((*p&#043;&#043; &#061; *src&#043;&#043;) !&#061; &#039;\\\\0&#039;); \/\/ \u9010\u5b57\u7b26\u62f7\u8d1d&#xff0c;\u5305\u542b&#039;\\\\0&#039;<br \/>\n    return dest; \/\/ \u8fd4\u56de\u76ee\u6807\u5730\u5740&#xff0c;\u652f\u6301\u94fe\u5f0f\u8c03\u7528<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char dest[20];<br \/>\n    my_strcpy(dest, &#034;Hello Copy&#034;);<br \/>\n    printf(&#034;%s\\\\n&#034;, dest); \/\/ Hello Copy<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>(*p&#043;&#043; &#061; *src&#043;&#043;) \u662f\u6838\u5fc3&#xff1a;\u5148\u8d4b\u503c\u3001\u518d\u79fb\u52a8\u6307\u9488&#xff1b;<\/li>\n<li>\u5faa\u73af\u7ec8\u6b62\u6761\u4ef6\u662f\u62f7\u8d1d\u4e86 &#039;\\\\0&#039;&#xff0c;\u4fdd\u8bc1\u76ee\u6807\u5b57\u7b26\u4e32\u5b8c\u6574\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>3. strncpy&#xff1a;\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b57\u7b26\u4e32\u62f7\u8d1d&#xff08;\u5b89\u5168\u7248&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>strcpy \u7684\u5b89\u5168\u7248\u672c&#xff0c;\u9650\u5236\u6700\u5927\u62f7\u8d1d\u5b57\u7b26\u6570 n&#xff0c;\u89e3\u51b3\u6ea2\u51fa\u95ee\u9898&#xff0c;\u6613\u9519\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u82e5\u6e90\u5b57\u7b26\u4e32\u957f\u5ea6 &lt; n&#xff1a;\u7528 &#039;\\\\0&#039; \u586b\u5145\u76ee\u6807\u81f3 n \u4e2a\u5b57\u7b26&#xff1b;<\/li>\n<li>\u82e5\u6e90\u5b57\u7b26\u4e32\u957f\u5ea6 \u2265 n&#xff1a;\u4ec5\u62f7\u8d1d\u524d n \u4e2a\u5b57\u7b26&#xff0c;\u76ee\u6807\u65e0\u81ea\u52a8 &#039;\\\\0&#039;&#xff08;\u9700\u624b\u52a8\u52a0&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u573a\u666f1&#xff1a;\u6e90\u957f\u5ea6 &lt; n<br \/>\n    char dest1[10];<br \/>\n    strncpy(dest1, &#034;Hi&#034;, 5); \/\/ \u62f7\u8d1d&#034;Hi&#034; &#043; 3\u4e2a&#039;\\\\0&#039;<br \/>\n    printf(&#034;dest1&#xff1a;%s&#xff08;\u957f\u5ea6%zu&#xff09;\\\\n&#034;, dest1, strlen(dest1)); \/\/ Hi&#xff08;2&#xff09;<\/p>\n<p>    \/\/ \u573a\u666f2&#xff1a;\u6e90\u957f\u5ea6 \u2265 n&#xff08;\u624b\u52a8\u52a0&#039;\\\\0&#039;&#xff09;<br \/>\n    char dest2[10];<br \/>\n    strncpy(dest2, &#034;HelloWorld&#034;, 5); \/\/ \u4ec5\u62f7\u8d1d&#034;Hello&#034;&#xff0c;\u65e0&#039;\\\\0&#039;<br \/>\n    dest2[5] &#061; &#039;\\\\0&#039;; \/\/ \u5fc5\u987b\u624b\u52a8\u8865&#039;\\\\0&#039;&#xff0c;\u5426\u5219\u4f1a\u4e71\u7801<br \/>\n    printf(&#034;dest2&#xff1a;%s\\\\n&#034;, dest2); \/\/ Hello<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>char *my_strncpy(char *dest, const char *src, size_t n) {<br \/>\n    assert(dest !&#061; NULL &amp;&amp; src !&#061; NULL);<br \/>\n    char *p &#061; dest;<br \/>\n    size_t i &#061; 0;<\/p>\n<p>    \/\/ \u7b2c\u4e00\u6b65&#xff1a;\u62f7\u8d1d\u6e90\u5b57\u7b26\u4e32&#xff0c;\u6700\u591an\u4e2a\u5b57\u7b26<br \/>\n    while (i &lt; n &amp;&amp; *src !&#061; &#039;\\\\0&#039;) {<br \/>\n        *p&#043;&#043; &#061; *src&#043;&#043;;<br \/>\n        i&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u6e90\u957f\u5ea6 &lt; n&#xff0c;\u7528&#039;\\\\0&#039;\u586b\u5145\u5269\u4f59\u4f4d\u7f6e<br \/>\n    while (i &lt; n) {<br \/>\n        *p&#043;&#043; &#061; &#039;\\\\0&#039;;<br \/>\n        i&#043;&#043;;<br \/>\n    }<\/p>\n<p>    return dest;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char dest1[10];<br \/>\n    my_strncpy(dest1, &#034;Test&#034;, 6);<br \/>\n    printf(&#034;dest1&#xff1a;%s\\\\n&#034;, dest1); \/\/ Test&#xff08;\u540e\u88652\u4e2a&#039;\\\\0&#039;&#xff09;<\/p>\n<p>    char dest2[10];<br \/>\n    my_strncpy(dest2, &#034;1234567890&#034;, 5);<br \/>\n    dest2[5] &#061; &#039;\\\\0&#039;; \/\/ \u624b\u52a8\u8865&#039;\\\\0&#039;<br \/>\n    printf(&#034;dest2&#xff1a;%s\\\\n&#034;, dest2); \/\/ 12345<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>\u5206\u4e24\u6b65\u5904\u7406&#xff1a;\u5148\u62f7\u8d1d\u6709\u6548\u5b57\u7b26&#xff0c;\u518d\u586b\u5145 &#039;\\\\0&#039;&#xff08;\u82e5\u9700\u8981&#xff09;&#xff1b;<\/li>\n<li>\u6e90\u957f\u5ea6 \u2265 n \u65f6&#xff0c;\u5fc5\u987b\u624b\u52a8\u7ed9\u76ee\u6807\u52a0 &#039;\\\\0&#039;&#xff0c;\u5426\u5219\u5b57\u7b26\u4e32\u65e0\u7ed3\u675f\u6807\u5fd7\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>4. strcat&#xff1a;\u5b57\u7b26\u4e32\u62fc\u63a5&#xff08;\u65e0\u957f\u5ea6\u9650\u5236&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u5c06\u6e90\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u76ee\u6807\u5b57\u7b26\u4e32\u672b\u5c3e&#xff08;\u8986\u76d6\u76ee\u6807\u539f\u6709 &#039;\\\\0&#039;&#xff0c;\u6700\u7ec8\u52a0\u65b0 &#039;\\\\0&#039;&#xff09;&#xff0c;\u6613\u6ea2\u51fa\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char dest[30] &#061; &#034;I love &#034;;<br \/>\n    strcat(dest, &#034;C Language&#034;);<br \/>\n    printf(&#034;\u62fc\u63a5\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest); \/\/ I love C Language<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>char *my_strcat(char *dest, const char *src) {<br \/>\n    assert(dest !&#061; NULL &amp;&amp; src !&#061; NULL);<br \/>\n    char *p &#061; dest;<\/p>\n<p>    \/\/ \u7b2c\u4e00\u6b65&#xff1a;\u627e\u5230\u76ee\u6807\u5b57\u7b26\u4e32\u7684&#039;\\\\0&#039;\u4f4d\u7f6e<br \/>\n    while (*p !&#061; &#039;\\\\0&#039;) {<br \/>\n        p&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u4ece&#039;\\\\0&#039;\u5f00\u59cb\u62f7\u8d1d\u6e90\u5b57\u7b26\u4e32&#xff08;\u540cstrcpy\u903b\u8f91&#xff09;<br \/>\n    while ((*p&#043;&#043; &#061; *src&#043;&#043;) !&#061; &#039;\\\\0&#039;);<br \/>\n    return dest;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char dest[30] &#061; &#034;Hello, &#034;;<br \/>\n    my_strcat(dest, &#034;World!&#034;);<br \/>\n    printf(&#034;%s\\\\n&#034;, dest); \/\/ Hello, World!<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>\u5148\u5b9a\u4f4d\u76ee\u6807\u7684 &#039;\\\\0&#039;&#xff0c;\u518d\u62fc\u63a5&#xff0c;\u907f\u514d\u8986\u76d6\u76ee\u6807\u539f\u6709\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>5. strncat&#xff1a;\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b57\u7b26\u4e32\u62fc\u63a5&#xff08;\u5b89\u5168\u7248&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>strcat \u7684\u5b89\u5168\u7248\u672c&#xff0c;\u9650\u5236\u6700\u591a\u62fc\u63a5 n \u4e2a\u5b57\u7b26&#xff0c;\u6838\u5fc3\u4f18\u52bf&#xff1a;<\/p>\n<ul>\n<li>\u65e0\u8bba\u6e90\u5b57\u7b26\u4e32\u591a\u957f&#xff0c;\u62fc\u63a5\u540e\u81ea\u52a8\u52a0 &#039;\\\\0&#039;&#xff08;\u65e0\u9700\u624b\u52a8\u8865&#xff09;&#xff1b;<\/li>\n<li>\u4ec5\u62fc\u63a5\u6e90\u5b57\u7b26\u4e32\u7684\u524d n \u4e2a\u5b57\u7b26&#xff08;\u4e0d\u542b\u6e90\u7684 &#039;\\\\0&#039;&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char dest[20] &#061; &#034;Hello, &#034;;<br \/>\n    strncat(dest, &#034;1234567890&#034;, 3); \/\/ \u4ec5\u62fc\u63a5\u524d3\u4e2a\u5b57\u7b26&#034;123&#034;<br \/>\n    printf(&#034;\u62fc\u63a5\u7ed3\u679c&#xff1a;%s\\\\n&#034;, dest); \/\/ Hello, 123<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>char *my_strncat(char *dest, const char *src, size_t n) {<br \/>\n    assert(dest !&#061; NULL &amp;&amp; src !&#061; NULL);<br \/>\n    char *p &#061; dest;<br \/>\n    size_t i &#061; 0;<\/p>\n<p>    \/\/ \u7b2c\u4e00\u6b65&#xff1a;\u627e\u5230\u76ee\u6807\u7684&#039;\\\\0&#039;<br \/>\n    while (*p !&#061; &#039;\\\\0&#039;) {<br \/>\n        p&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u62fc\u63a5\u6700\u591an\u4e2a\u5b57\u7b26&#xff0c;\u4e0d\u5305\u542b\u6e90\u7684&#039;\\\\0&#039;<br \/>\n    while (i &lt; n &amp;&amp; *src !&#061; &#039;\\\\0&#039;) {<br \/>\n        *p&#043;&#043; &#061; *src&#043;&#043;;<br \/>\n        i&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e09\u6b65&#xff1a;\u624b\u52a8\u52a0&#039;\\\\0&#039;&#xff08;\u6838\u5fc3\u5b89\u5168\u70b9&#xff09;<br \/>\n    *p &#061; &#039;\\\\0&#039;;<br \/>\n    return dest;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char dest[20] &#061; &#034;Test: &#034;;<br \/>\n    my_strncat(dest, &#034;ABCDEFG&#034;, 4);<br \/>\n    printf(&#034;%s\\\\n&#034;, dest); \/\/ Test: ABCD<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>\u62fc\u63a5\u540e\u5f3a\u5236\u52a0 &#039;\\\\0&#039;&#xff0c;\u662f strncat \u6bd4 strncpy \u66f4\u5b89\u5168\u7684\u6838\u5fc3&#xff1b;<\/li>\n<li>\u53ea\u62fc\u63a5\u6e90\u7684\u524d n \u4e2a\u6709\u6548\u5b57\u7b26&#xff0c;\u4e0d\u4f1a\u62f7\u8d1d\u6e90\u7684 &#039;\\\\0&#039;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>6. strcmp&#xff1a;\u5b57\u7b26\u4e32\u6bd4\u8f83&#xff08;\u65e0\u957f\u5ea6\u9650\u5236&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u6309 ASCII \u7801\u9010\u5b57\u7b26\u6bd4\u8f83&#xff0c;\u8fd4\u56de\u503c\u89c4\u5219&#xff1a;<\/p>\n<ul>\n<li>0&#xff1a;\u4e24\u5b57\u7b26\u4e32\u76f8\u7b49&#xff1b;<\/li>\n<li>&lt;0&#xff1a;str1 \u5c0f\u4e8e str2&#xff1b;<\/li>\n<li>\n<p>0&#xff1a;str1 \u5927\u4e8e str2\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    printf(&#034;%d\\\\n&#034;, strcmp(&#034;Apple&#034;, &#034;Banana&#034;)); \/\/ -1&#xff08;A(65)&lt;B(66)&#xff09;<br \/>\n    printf(&#034;%d\\\\n&#034;, strcmp(&#034;Apple&#034;, &#034;Apple&#034;));  \/\/ 0<br \/>\n    printf(&#034;%d\\\\n&#034;, strcmp(&#034;Banana&#034;, &#034;Apple&#034;)); \/\/ 1<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>int my_strcmp(const char *str1, const char *str2) {<br \/>\n    assert(str1 !&#061; NULL &amp;&amp; str2 !&#061; NULL);<br \/>\n    \/\/ \u904d\u5386\u5230\u5b57\u7b26\u4e0d\u540c\u6216\u5230&#039;\\\\0&#039;<br \/>\n    while (*str1 !&#061; &#039;\\\\0&#039; &amp;&amp; *str2 !&#061; &#039;\\\\0&#039; &amp;&amp; *str1 &#061;&#061; *str2) {<br \/>\n        str1&#043;&#043;;<br \/>\n        str2&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u8f6c\u4e3aunsigned char\u907f\u514d\u8d1f\u6570\u6ea2\u51fa&#xff08;\u5982ASCII 0~255&#xff09;<br \/>\n    return (unsigned char)*str1 &#8211; (unsigned char)*str2;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    printf(&#034;%d\\\\n&#034;, my_strcmp(&#034;Cat&#034;, &#034;Car&#034;)); \/\/ 2&#xff08;t(116)-r(114)&#xff09;<br \/>\n    printf(&#034;%d\\\\n&#034;, my_strcmp(&#034;Car&#034;, &#034;Cat&#034;)); \/\/ -2<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>unsigned char \u8f6c\u6362&#xff1a;\u907f\u514d\u5b57\u7b26 ASCII \u503c\u4e3a\u8d1f\u6570\u65f6\u7684\u8ba1\u7b97\u9519\u8bef&#xff1b;<\/li>\n<li>\u5faa\u73af\u7ec8\u6b62\u6761\u4ef6&#xff1a;\u5b57\u7b26\u4e0d\u540c \u6216 \u4efb\u4e00\u5b57\u7b26\u4e32\u5230\u672b\u5c3e\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>7. strncmp&#xff1a;\u5e26\u957f\u5ea6\u9650\u5236\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83&#xff08;\u5b89\u5168\u7248&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u6700\u591a\u6bd4\u8f83\u524d n \u4e2a\u5b57\u7b26&#xff0c;\u907f\u514d\u8d85\u957f\u5b57\u7b26\u4e32\u7684\u65e0\u6548\u6bd4\u8f83\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u524d3\u4e2a\u5b57\u7b26\u90fd\u662f&#034;App&#034;&#xff0c;\u8fd4\u56de0<br \/>\n    printf(&#034;%d\\\\n&#034;, strncmp(&#034;Apple&#034;, &#034;Application&#034;, 3));<br \/>\n    \/\/ \u524d2\u4e2a\u5b57\u7b26&#034;Ap&#034;&#061;&#061;&#034;Ap&#034;&#xff0c;\u7b2c3\u4e2a&#039;p&#039;&gt;&#039;p&#039;? \u4e0d&#xff0c;\u7b2c3\u4e2a&#039;p&#039;&#061;&#061;&#039;p&#039;&#xff0c;\u7b2c4\u4e2a&#039;l&#039;&lt;&#039;l&#039;? \u793a\u4f8b&#xff1a;strncmp(&#034;Apple&#034;, &#034;App&#034;, 3) \u2192 0<br \/>\n    printf(&#034;%d\\\\n&#034;, strncmp(&#034;Apple&#034;, &#034;App&#034;, 3)); \/\/ 0<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>int my_strncmp(const char *str1, const char *str2, size_t n) {<br \/>\n    assert(str1 !&#061; NULL &amp;&amp; str2 !&#061; NULL);<br \/>\n    if (n &#061;&#061; 0) return 0; \/\/ \u6bd4\u8f830\u4e2a\u5b57\u7b26&#xff0c;\u76f4\u63a5\u8fd4\u56de\u76f8\u7b49<\/p>\n<p>    size_t i &#061; 0;<br \/>\n    \/\/ \u6700\u591a\u6bd4\u8f83n\u4e2a\u5b57\u7b26&#xff0c;\u4e14\u5b57\u7b26\u76f8\u7b49\u3001\u672a\u5230&#039;\\\\0&#039;<br \/>\n    while (i &lt; n-1 &amp;&amp; *str1 !&#061; &#039;\\\\0&#039; &amp;&amp; *str2 !&#061; &#039;\\\\0&#039; &amp;&amp; *str1 &#061;&#061; *str2) {<br \/>\n        str1&#043;&#043;;<br \/>\n        str2&#043;&#043;;<br \/>\n        i&#043;&#043;;<br \/>\n    }<\/p>\n<p>    return (unsigned char)*str1 &#8211; (unsigned char)*str2;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    printf(&#034;%d\\\\n&#034;, my_strncmp(&#034;12345&#034;, &#034;123678&#034;, 3)); \/\/ 0&#xff08;\u524d3\u4e2a\u76f8\u7b49&#xff09;<br \/>\n    printf(&#034;%d\\\\n&#034;, my_strncmp(&#034;12345&#034;, &#034;123678&#034;, 4)); \/\/ -2&#xff08;4&lt;6&#xff09;<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>i &lt; n-1&#xff1a;\u9884\u7559\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u6bd4\u8f83&#xff08;\u907f\u514d\u8d8a\u754c&#xff09;&#xff1b;<\/li>\n<li>n&#061;0 \u76f4\u63a5\u8fd4\u56de 0&#xff0c;\u7b26\u5408\u6807\u51c6\u5e93\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>8. strchr&#xff1a;\u67e5\u627e\u5b57\u7b26\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6307\u5b9a\u5b57\u7b26\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u8be5\u4f4d\u7f6e\u6307\u9488&#xff1b;\u627e\u4e0d\u5230\u8fd4\u56de NULL\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str[] &#061; &#034;Hello World&#034;;<br \/>\n    char *p &#061; strchr(str, &#039;o&#039;);<br \/>\n    if (p !&#061; NULL) {<br \/>\n        printf(&#034;\u627e\u5230&#xff1a;%s\\\\n&#034;, p);       \/\/ o World<br \/>\n        printf(&#034;\u7d22\u5f15&#xff1a;%zu\\\\n&#034;, p &#8211; str); \/\/ 4<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>char *my_strchr(const char *str, int ch) {<br \/>\n    assert(str !&#061; NULL);<br \/>\n    unsigned char target &#061; (unsigned char)ch; \/\/ \u7edf\u4e00\u5b57\u7b26\u7c7b\u578b<\/p>\n<p>    while (*str !&#061; &#039;\\\\0&#039;) {<br \/>\n        if ((unsigned char)*str &#061;&#061; target) {<br \/>\n            return (char *)str; \/\/ \u627e\u5230\u8fd4\u56de\u6307\u9488<br \/>\n        }<br \/>\n        str&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/ \u7279\u6b8a\u60c5\u51b5&#xff1a;\u67e5\u627e\u7684\u662f&#039;\\\\0&#039;<br \/>\n    if (target &#061;&#061; &#039;\\\\0&#039;) {<br \/>\n        return (char *)str;<br \/>\n    }<\/p>\n<p>    return NULL; \/\/ \u672a\u627e\u5230<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char *p &#061; my_strchr(&#034;Programming&#034;, &#039;g&#039;);<br \/>\n    printf(&#034;%s\\\\n&#034;, p); \/\/ gramming<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>\u53c2\u6570 ch \u662f int \u7c7b\u578b&#xff1a;\u652f\u6301\u4f20\u5165 EOF&#xff08;\u503c\u4e3a &#8211; 1&#xff09;&#xff0c;\u9700\u8f6c\u4e3a unsigned char&#xff1b;<\/li>\n<li>\u652f\u6301\u67e5\u627e &#039;\\\\0&#039;&#xff08;\u8fd4\u56de\u5b57\u7b26\u4e32\u672b\u5c3e\u6307\u9488&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>9. strstr&#xff1a;\u67e5\u627e\u5b50\u4e32\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u5728\u5b57\u7b26\u4e32 str \u4e2d\u67e5\u627e\u5b50\u4e32 sub \u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u6307\u9488&#xff1b;\u627e\u4e0d\u5230\u8fd4\u56de NULL\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str[] &#061; &#034;I love C Programming&#034;;<br \/>\n    char *p &#061; strstr(str, &#034;Programming&#034;);<br \/>\n    if (p !&#061; NULL) {<br \/>\n        printf(&#034;\u627e\u5230\u5b50\u4e32&#xff1a;%s\\\\n&#034;, p); \/\/ Programming<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>char *my_strstr(const char *str, const char *sub) {<br \/>\n    assert(str !&#061; NULL &amp;&amp; sub !&#061; NULL);<br \/>\n    if (*sub &#061;&#061; &#039;\\\\0&#039;) return (char *)str; \/\/ \u7a7a\u5b50\u4e32\u8fd4\u56de\u539f\u5b57\u7b26\u4e32<\/p>\n<p>    const char *p_str &#061; str;<br \/>\n    const char *p_sub;<\/p>\n<p>    \/\/ \u904d\u5386\u539f\u5b57\u7b26\u4e32<br \/>\n    while (*p_str !&#061; &#039;\\\\0&#039;) {<br \/>\n        p_sub &#061; sub;<br \/>\n        const char *temp &#061; p_str;<\/p>\n<p>        \/\/ \u5339\u914d\u5b50\u4e32<br \/>\n        while (*temp !&#061; &#039;\\\\0&#039; &amp;&amp; *p_sub !&#061; &#039;\\\\0&#039; &amp;&amp; *temp &#061;&#061; *p_sub) {<br \/>\n            temp&#043;&#043;;<br \/>\n            p_sub&#043;&#043;;<br \/>\n        }<\/p>\n<p>        \/\/ \u5b50\u4e32\u5168\u90e8\u5339\u914d\u5b8c\u6210<br \/>\n        if (*p_sub &#061;&#061; &#039;\\\\0&#039;) {<br \/>\n            return (char *)p_str;<br \/>\n        }<\/p>\n<p>        p_str&#043;&#043;;<br \/>\n    }<\/p>\n<p>    return NULL; \/\/ \u672a\u627e\u5230<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char *p &#061; my_strstr(&#034;HelloWorldHello&#034;, &#034;World&#034;);<br \/>\n    printf(&#034;%s\\\\n&#034;, p); \/\/ WorldHello<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>\u53cc\u5c42\u5faa\u73af&#xff1a;\u5916\u5c42\u904d\u5386\u539f\u5b57\u7b26\u4e32&#xff0c;\u5185\u5c42\u5339\u914d\u5b50\u4e32&#xff1b;<\/li>\n<li>\u7a7a\u5b50\u4e32&#xff08;*sub &#061;&#061; &#039;\\\\0&#039;&#xff09;\u76f4\u63a5\u8fd4\u56de\u539f\u5b57\u7b26\u4e32&#xff08;\u6807\u51c6\u5e93\u89c4\u5219&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>10. strtok&#xff1a;\u5b57\u7b26\u4e32\u5206\u5272&#xff08;\u6309\u5206\u9694\u7b26\u62c6\u5206&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u6309\u6307\u5b9a\u5206\u9694\u7b26&#xff08;\u5982 ,\u3001&#xff09;\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u591a\u4e2a\u5b50\u4e32&#xff0c;\u6ce8\u610f&#xff1a;<\/p>\n<ul>\n<li>\u4f1a\u4fee\u6539\u539f\u5b57\u7b26\u4e32&#xff08;\u5c06\u5206\u9694\u7b26\u66ff\u6362\u4e3a &#039;\\\\0&#039;&#xff09;&#xff1b;<\/li>\n<li>\u9996\u6b21\u8c03\u7528\u4f20\u539f\u5b57\u7b26\u4e32&#xff0c;\u540e\u7eed\u8c03\u7528\u4f20 NULL\u3002<\/li>\n<\/ul>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char str[] &#061; &#034;Apple,Banana,Cherry&#034;; \/\/ \u5fc5\u987b\u662f\u53ef\u4fee\u6539\u7684\u6570\u7ec4&#xff08;\u4e0d\u80fd\u662f\u5e38\u91cf\u5b57\u7b26\u4e32&#xff09;<br \/>\n    char *token &#061; strtok(str, &#034;,&#034;);     \/\/ \u9996\u6b21\u8c03\u7528&#xff1a;\u4f20\u539f\u5b57\u7b26\u4e32&#043;\u5206\u9694\u7b26<br \/>\n    while (token !&#061; NULL) {<br \/>\n        printf(&#034;\u5206\u5272\u7ed3\u679c&#xff1a;%s\\\\n&#034;, token); \/\/ Apple \u2192 Banana \u2192 Cherry<br \/>\n        token &#061; strtok(NULL, &#034;,&#034;);      \/\/ \u540e\u7eed\u8c03\u7528&#xff1a;\u4f20NULL<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801&#xff08;\u7b80\u5316\u7248&#xff09;<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>\/\/ \u9759\u6001\u53d8\u91cf\u4fdd\u5b58\u4e0a\u6b21\u5206\u5272\u7684\u4f4d\u7f6e<br \/>\nstatic char *last_pos &#061; NULL;<\/p>\n<p>char *my_strtok(char *str, const char *delim) {<br \/>\n    assert(delim !&#061; NULL);<br \/>\n    if (str &#061;&#061; NULL) {<br \/>\n        str &#061; last_pos; \/\/ \u540e\u7eed\u8c03\u7528&#xff1a;\u4ece\u4e0a\u6b21\u4f4d\u7f6e\u7ee7\u7eed<br \/>\n        if (str &#061;&#061; NULL) return NULL; \/\/ \u65e0\u5269\u4f59\u5b57\u7b26<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e00\u6b65&#xff1a;\u8df3\u8fc7\u5f00\u5934\u7684\u5206\u9694\u7b26<br \/>\n    while (*str !&#061; &#039;\\\\0&#039; &amp;&amp; strchr(delim, *str) !&#061; NULL) {<br \/>\n        str&#043;&#043;;<br \/>\n    }<\/p>\n<p>    \/\/ \u65e0\u6709\u6548\u5b57\u7b26<br \/>\n    if (*str &#061;&#061; &#039;\\\\0&#039;) {<br \/>\n        last_pos &#061; NULL;<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u627e\u5230\u5206\u9694\u7b26\u4f4d\u7f6e&#xff0c;\u66ff\u6362\u4e3a&#039;\\\\0&#039;<br \/>\n    char *token_start &#061; str;<br \/>\n    while (*str !&#061; &#039;\\\\0&#039; &amp;&amp; strchr(delim, *str) &#061;&#061; NULL) {<br \/>\n        str&#043;&#043;;<br \/>\n    }<\/p>\n<p>    if (*str !&#061; &#039;\\\\0&#039;) {<br \/>\n        *str &#061; &#039;\\\\0&#039;;       \/\/ \u5206\u9694\u7b26\u66ff\u6362\u4e3a&#039;\\\\0&#039;<br \/>\n        last_pos &#061; str &#043; 1; \/\/ \u4fdd\u5b58\u4e0b\u6b21\u5206\u5272\u7684\u8d77\u59cb\u4f4d\u7f6e<br \/>\n    } else {<br \/>\n        last_pos &#061; NULL;   \/\/ \u5230\u672b\u5c3e&#xff0c;\u4e0b\u6b21\u8fd4\u56deNULL<br \/>\n    }<\/p>\n<p>    return token_start;<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char str[] &#061; &#034;123 456 789&#034;;<br \/>\n    char *token &#061; my_strtok(str, &#034; &#034;);<br \/>\n    while (token !&#061; NULL) {<br \/>\n        printf(&#034;%s\\\\n&#034;, token); \/\/ 123 \u2192 456 \u2192 789<br \/>\n        token &#061; my_strtok(NULL, &#034; &#034;);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u903b\u8f91<\/h4>\n<ul>\n<li>static char *last_pos&#xff1a;\u4fdd\u5b58\u4e0a\u6b21\u5206\u5272\u7684\u4f4d\u7f6e&#xff0c;\u5b9e\u73b0 \u201c\u540e\u7eed\u8c03\u7528\u4f20 NULL\u201d \u7684\u903b\u8f91&#xff1b;<\/li>\n<li>\u539f\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u53ef\u4fee\u6539\u7684&#xff08;\u4e0d\u80fd\u662f const char *&#xff09;&#xff0c;\u5426\u5219\u4f1a\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>11. \u8f85\u52a9\u51fd\u6570&#xff1a;memset&#xff08;\u5185\u5b58\u586b\u5145&#xff0c;\u521d\u59cb\u5316\u5b57\u7b26\u4e32&#xff09;<\/h3>\n<h4>\u529f\u80fd\u8bf4\u660e<\/h4>\n<p>\u5c06\u6307\u5b9a\u5185\u5b58\u533a\u57df\u586b\u5145\u4e3a\u6307\u5b9a\u5b57\u7b26&#xff08;\u5e38\u7528\u4e8e\u5b57\u7b26\u4e32\u521d\u59cb\u5316&#xff0c;\u5982\u6e05\u7a7a\u7f13\u51b2\u533a&#xff09;\u3002<\/p>\n<h4>\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    char dest[20];<br \/>\n    memset(dest, 0, sizeof(dest)); \/\/ \u5168\u90e8\u586b\u5145\u4e3a&#039;\\\\0&#039;<br \/>\n    strcpy(dest, &#034;Test&#034;);<br \/>\n    printf(&#034;%s\\\\n&#034;, dest); \/\/ Test<\/p>\n<p>    memset(dest, &#039;*&#039;, 5); \/\/ \u524d5\u4e2a\u5b57\u7b26\u586b\u5145\u4e3a&#039;*&#039;<br \/>\n    printf(&#034;%s\\\\n&#034;, dest); \/\/ *****<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>\u624b\u52a8\u5b9e\u73b0\u6e90\u4ee3\u7801<\/h4>\n<p>c<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;assert.h&gt;<\/p>\n<p>void *my_memset(void *ptr, int value, size_t num) {<br \/>\n    assert(ptr !&#061; NULL);<br \/>\n    unsigned char *p &#061; (unsigned char *)ptr;<br \/>\n    unsigned char val &#061; (unsigned char)value;<\/p>\n<p>    while (num&#8211; &gt; 0) {<br \/>\n        *p&#043;&#043; &#061; val;<br \/>\n    }<\/p>\n<p>    return ptr; \/\/ \u8fd4\u56de\u539f\u6307\u9488&#xff0c;\u652f\u6301\u94fe\u5f0f\u8c03\u7528<br \/>\n}<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    char dest[20];<br \/>\n    my_memset(dest, 0, sizeof(dest));<br \/>\n    my_memset(dest, &#039;#&#039;, 3);<br \/>\n    printf(&#034;%s\\\\n&#034;, dest); \/\/ ###<br \/>\n    return 0;<br \/>\n}<\/p>\n<hr \/>\n<h4>\u603b\u7ed3<\/h4>\n<li>\u5b89\u5168\u6838\u5fc3&#xff1a;\n<ul>\n<li>\u6240\u6709\u51fd\u6570\u5fc5\u987b\u5148\u68c0\u67e5\u6307\u9488\u662f\u5426\u4e3a NULL&#xff08;assert \u6216\u6761\u4ef6\u5224\u65ad&#xff09;&#xff1b;<\/li>\n<li>\u5e26 n \u7684\u51fd\u6570&#xff08;strncpy\/strncat\/strncmp&#xff09;\u4f18\u5148\u4f7f\u7528&#xff0c;\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa&#xff1b;<\/li>\n<li>strncpy \u9700\u624b\u52a8\u8865 &#039;\\\\0&#039;&#xff0c;strncat \u81ea\u52a8\u8865 &#039;\\\\0&#039;&#xff08;\u66f4\u5b89\u5168&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6838\u5fc3\u89c4\u5219&#xff1a;\n<ul>\n<li>C \u5b57\u7b26\u4e32\u4ee5 &#039;\\\\0&#039; \u7ed3\u5c3e&#xff0c;\u6240\u6709\u5904\u7406\u903b\u8f91\u5fc5\u987b\u56f4\u7ed5 &#039;\\\\0&#039;&#xff1b;<\/li>\n<li>\u6307\u9488\u64cd\u4f5c\u662f\u5b57\u7b26\u4e32\u51fd\u6570\u7684\u6838\u5fc3&#xff0c;\u7406\u89e3 *p&#043;&#043; \u7b49\u64cd\u4f5c\u662f\u5173\u952e&#xff1b;<\/li>\n<li>strtok \u4f1a\u4fee\u6539\u539f\u5b57\u7b26\u4e32&#xff0c;\u4f7f\u7528\u65f6\u9700\u6ce8\u610f\u539f\u5b57\u7b26\u4e32\u7684\u53ef\u4fee\u6539\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4f7f\u7528\u5efa\u8bae&#xff1a;\n<ul>\n<li>\u5b9e\u9645\u5f00\u53d1\u4e2d\u4f18\u5148\u7528\u6807\u51c6\u5e93\u51fd\u6570&#xff08;\u7ecf\u8fc7\u4e25\u683c\u6d4b\u8bd5&#xff09;&#xff1b;<\/li>\n<li>\u624b\u52a8\u5b9e\u73b0\u4ec5\u7528\u4e8e\u7406\u89e3\u5e95\u5c42\u903b\u8f91&#xff0c;\u751f\u4ea7\u73af\u5883\u907f\u514d\u91cd\u590d\u9020\u8f6e\u5b50\u3002<\/li>\n<\/ul>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001strlen&#xff1a;\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u529f\u80fd\u8bf4\u660estrlen \u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u6709\u6548\u957f\u5ea6&#xff08;\u4e0d\u5305\u542b\u5b57\u7b26\u4e32\u7ec8\u6b62\u7b26 \\\\0&#xff09;&#xff0c;\u533a\u522b\u4e8e sizeof&#xff08;\u8ba1\u7b97\u5185\u5b58\u5927\u5c0f&#xff09;\u3002\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8bc\u8fd0\u884c#include<br \/>\n#include int main() {char str[]  \\&#8221;Hello World\\&#8221;;\/\/ \u8f93\u51fa 11&#xff08;H\u5230d\u517111\u4e2a\u5b57\u7b26&#xff0c;\u4e0d\u542b\\\\0&amp;#<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,174,427],"topic":[],"class_list":{"0":"post-62456","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-c","9":"tag-427"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/62456.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001strlen&#xff1a;\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u529f\u80fd\u8bf4\u660estrlen \u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u6709\u6548\u957f\u5ea6&#xff08;\u4e0d\u5305\u542b\u5b57\u7b26\u4e32\u7ec8\u6b62\u7b26 \\0&#xff09;&#xff0c;\u533a\u522b\u4e8e sizeof&#xff08;\u8ba1\u7b97\u5185\u5b58\u5927\u5c0f&#xff09;\u3002\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8bc\u8fd0\u884c#include #include int main() {char str[] &quot;Hello World&quot;;\/\/ \u8f93\u51fa 11&#xff08;H\u5230d\u517111\u4e2a\u5b57\u7b26&#xff0c;\u4e0d\u542b\\0&amp;#\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/62456.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-19T16:34:07+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62456.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/62456.html\",\"name\":\"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-19T16:34:07+00:00\",\"dateModified\":\"2026-01-19T16:34:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/62456.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/62456.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62456.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/62456.html","og_locale":"zh_CN","og_type":"article","og_title":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001strlen&#xff1a;\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u529f\u80fd\u8bf4\u660estrlen \u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u6709\u6548\u957f\u5ea6&#xff08;\u4e0d\u5305\u542b\u5b57\u7b26\u4e32\u7ec8\u6b62\u7b26 \\0&#xff09;&#xff0c;\u533a\u522b\u4e8e sizeof&#xff08;\u8ba1\u7b97\u5185\u5b58\u5927\u5c0f&#xff09;\u3002\u6807\u51c6\u5e93\u4f7f\u7528\u5b9e\u4f8bc\u8fd0\u884c#include #include int main() {char str[] \"Hello World\";\/\/ \u8f93\u51fa 11&#xff08;H\u5230d\u517111\u4e2a\u5b57\u7b26&#xff0c;\u4e0d\u542b\\0&amp;#","og_url":"https:\/\/www.wsisp.com\/helps\/62456.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-19T16:34:07+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"14 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/62456.html","url":"https:\/\/www.wsisp.com\/helps\/62456.html","name":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-19T16:34:07+00:00","dateModified":"2026-01-19T16:34:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/62456.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/62456.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/62456.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u539f\u51fd\u6570"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=62456"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62456\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=62456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=62456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=62456"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=62456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}