{"id":405,"date":"2023-02-25T10:50:41","date_gmt":"2023-02-25T02:50:41","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=405"},"modified":"2023-04-29T21:36:52","modified_gmt":"2023-04-29T13:36:52","slug":"android-ndk-basic-c-character-string","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-c-character-string\/","title":{"rendered":"Android NDK\u57fa\u78405\uff1aC_\u5b57\u7b26\u4e32"},"content":{"rendered":"<h2>\u5b57\u7b26\u4e32<\/h2>\n<pre><code class=\"language-c\">\/\/\u4f7f\u7528\u5b57\u7b26\u6570\u7ec4\u5b58\u50a8\u5b57\u7b26\u4e32\nvoid main() {\n    \/\/char str[] = {&#039;c&#039;, &#039;h&#039;, &#039;i&#039;, &#039;n&#039;, &#039;a&#039;, &#039;\\0&#039;};\n    \/\/char str[6] = { &#039;c&#039;, &#039;h&#039;, &#039;i&#039;, &#039;n&#039;, &#039;a&#039; };\n    char str[10] = &quot;china&quot;;\n    \/\/\u53ef\u4ee5\u4fee\u6539\n    str[0] = &#039;s&#039;;\n\n    printf(&quot;%s\\n&quot;, str);\n    printf(&quot;%#x\\n&quot;, str);\n    getchar();\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-c\">\/\/\u5b57\u7b26\u6307\u9488\nvoid main() {\n    \/\/\u5185\u5b58\u8fde\u7eed\u6392\u5217\n    char *str = &quot;how are you?&quot;;\n\n    \/\/\u4e0d\u53ef\u4ee5\u4fee\u6539\n    \/\/str[0] = &#039;w&#039;\n    \/\/str += 1;\n    \/\/*str = &#039;y&#039;;\n    printf(&quot;%s\\n&quot;, str);\n    printf(&quot;%#x\\n&quot;, str);\n\n    \/\/\u4f7f\u7528\u6307\u9488\u52a0\u6cd5\uff0c\u622a\u53d6\u5b57\u7b26\u4e32\n    str += 3;\n    while (*str) {\n        printf(&quot;%c&quot;, *str);\n        str++;\n    }\n    printf(&quot;\\n\u7ed3\u675f&quot;);\n    getchar();\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u5b57\u7b26\u6570\u503c\u4e0e\u5b57\u7b26\u6307\u9488\u7684\u533a\u522b\nvoid main() {\n    char a[10] = &quot;Happy&quot;;  \/\/\u5b57\u7b26\u6570\u7ec4\u53ea\u80fd\u5728\u58f0\u660e\u65f6\u8d4b\u503c\n    \/\/a = &quot;Sad&quot;;  \/\/\u62a5\u9519\n    a[0] - &#039;A&#039;;  \/\/\u53ef\u4ee5\u4fee\u6539\u5185\u5bb9\n    strcpy(a, &quot;Sad&quot;);  \/\/\u91cd\u65b0\u8d4b\u503c\n\n    \/\/\u5b57\u7b26\u6307\u9488\uff0c\u53ef\u4ee5\u591a\u6b21\u8d4b\u503c\u5b57\u7b26\u4e32\n    char *b = &quot;Friend&quot;;\n    b = &quot;Family&quot;;  \/\/\u53ef\u4ee5\u91cd\u65b0\u8d4b\u503c\n    \/\/*b = &#039;B&#039;;  \/\/\u62a5\u9519\uff0c\u4e0d\u80fd\u4fee\u6539\u5b57\u7b26\u5185\u5bb9\n\n    getchar();\n}<\/code><\/pre>\n<h2>\u5b57\u7b26\u4e32\u76f8\u5173\u51fd\u6570<\/h2>\n<pre><code class=\"language-c\">\/\/strcat\u5b57\u7b26\u4e32\u62fc\u63a5\u51fd\u6570\n\/\/\u5728\u7ebfAPI\u6587\u6863\uff1ahttp:\/\/www.kuqin.com\/clib\/string\/strcpy.html\nvoid main(void) {\n    char dest[50];    \n    char *a = &quot;china&quot;;\n    char *b = &quot; is powerful!&quot;;\n    strcpy(dest, a);\n    strcat(dest, b);\n    printf(&quot;%s\\n&quot;, dest);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/strchr\u5728\u4e00\u4e2a\u4e32\u4e2d\u67e5\u627e\u7ed9\u5b9a\u5b57\u7b26\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u4e4b\u5904\nvoid main(void) {\n    char *str = &quot;I want go to USA!&quot;;\n    printf(&quot;%#x\\n&quot;, str);\n    \/\/w\u5143\u7d20\u7684\u6307\u9488\n    \/\/str+3\n    char* p = strchr(str, &#039;w&#039;);\n    if (p) {\n        printf(&quot;\u7d22\u5f15\u4f4d\u7f6e\uff1a%d\\n&quot;, p - str);\n    } else {\n        printf(&quot;\u6ca1\u6709\u627e\u5230&quot;);\n    }\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/strstr \u4ece\u5b57\u7b26\u4e32haystack\u4e2d\u5bfb\u627eneedle\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\nvoid main(void) {\n    char *haystack = &quot;I want go to USA!&quot;;\n    char *needle = &quot;to&quot;;\n    \/\/\u67e5\u627e\u5b57\u7b26\u4e32\n    char* p = strstr(haystack, needle);\n    if (p) {\n        printf(&quot;\u7d22\u5f15\u4f4d\u7f6e\uff1a%d\\n&quot;, p - haystack);\n    } else {\n        printf(&quot;\u6ca1\u6709\u627e\u5230&quot;);\n    }\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/strcmp \u6bd4\u8f83\u5b57\u7b26\u4e32\uff0c\u533a\u5206\u5927\u5c0f\u5199\n\/\/strcmpi \u6bd4\u8f83\u5b57\u7b26\u4e32\uff0c\u5ffd\u7565\u5927\u5c0f\u5199\nvoid main(void) {\n    char *str1 = &quot;abc&quot;;\n    char *str2 = &quot;ABC&quot;;\n    \/\/int r = strcmp(str1, str2);\n    \/\/int r = strcmpi(str1, str2);\n    int r = _strcmpi(str1, str2);\n    printf(&quot;%d\\n&quot;, r);\n    if (r &gt; 0) {\n        printf(&quot;str1 \u5927\u4e8estr2\\n&quot;);\n    } else if (r == 0) {\n        printf(&quot;str1 \u7b49\u4e8estr2\\n&quot;);\n    } else {\n        printf(&quot;str1 \u5c0f\u4e8estr2\\n&quot;);\n    }\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/strset \u628a\u5b57\u7b26\u4e32s\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u8bbe\u7f6e\u6210\u5b57\u7b26c\nvoid main(void) {\n    char str[] = &quot;internet change the world!&quot;;\n    _strset(str, &#039;w&#039;);\n    printf(&quot;%s\\n&quot;, str);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/strrev \u628a\u5b57\u7b26\u4e32s\u7684\u6240\u6709\u5b57\u7b26\u7684\u987a\u5e8f\u98a0\u5012\u8fc7\u6765\nvoid main(void) {\n    char str[] = &quot;internet change the world!&quot;;\n    _strrev(str);\n    printf(&quot;%s\\n&quot;, str);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/atoi \u5b57\u7b26\u4e32\u8f6c\u4e3aint\u7c7b\u578b\n\/\/atol()\uff1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u957f\u6574\u578b\u503c\nvoid main(void) {\n    char* str = &quot;a78&quot;;\n    \/\/int r = atoi(str);    \n    printf(&quot;%d\\n&quot;, r);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/ \u5b57\u7b26\u4e32\u8f6c\u4e3adouble\u7c7b\u578b\nvoid main(void) {\n    char* str = &quot;77b8b&quot;;\n    char** p = NULL;\n    \/\/char* p = str + 2;\n    \/\/\u53c2\u6570\u8bf4\u660e\uff1astr\u4e3a\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\uff0cendstr \u4e3a\u7b2c\u4e00\u4e2a\u4e0d\u80fd\u8f6c\u6362\u7684\u5b57\u7b26\u7684\u6307\u9488\n    double r = strtod(str, p);\n    printf(&quot;%lf\\n&quot;, r);\n    printf(&quot;%#x\\n&quot;, p);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/strupr\u8f6c\u6362\u4e3a\u5927\u5199\nvoid main(void) {\n    char str[] = &quot;CHINA motherland!&quot;;\n    _strupr(str);\n    printf(&quot;%s\\n&quot;,str);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u8f6c\u6362\u4e3a\u5c0f\u5199\nvoid mystrlwr(char str[], int len) {\n    int i = 0;\n    for (; i &lt; len; i++) {\n        \/\/A-Z \u5b57\u6bcd a-Z\n        if (str[i] &gt;= &#039;A&#039; &amp;&amp; str[i] &lt;= &#039;Z&#039;) {\n            str[i] = str[i]-&#039;A&#039; + &#039;a&#039;;\n        }\n    }    \n}\n\nvoid main(void) {\n    char str[] = &quot;CHINA motherland!&quot;;\n    mystrlwr(str, strlen(str));\n    printf(&quot;%s\\n&quot;, str);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u7ec3\u4e60\uff1a\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u7684\u5b57\u7b26\nvoid main(void) {\n    char str[] = &quot;CHINA motherland!&quot;;\n    mystrlwr(str, strlen(str));\n    printf(&quot;%s\\n&quot;, str);\n    system(&quot;pause&quot;);\n}\n\nint main() {\n    char str[] = &quot;vencent ppqq&quot;;\n\n    delchar(str, &#039;t&#039;);\n    printf(&quot;%s\\n&quot;, str);\n\n    system(&quot;pause&quot;);\n}\n\nvoid delchar(char *str, char del) {\n    char *p = str;\n    while (*str != &#039;\\0&#039;) {\n        if (*str != del) {\n            *p++ = *str;\n        }\n        str++;\n    }\n    *p = &#039;\\0&#039;;\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u7ec3\u4e60\uff1a\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\n\/\/Java String replaceAll \n\/\/StringBuffer buff.deleteCharAt(buff.length()-1);\nvoid main(void) {\n    char str[] = &quot;internet,&quot;;\n    str[strlen(str) - 1] = &#039;\\0&#039;;\n    printf(&quot;%s\\n&quot;, str);\n\n    \/\/\u4f5c\u4e1a\uff1arealloc\u5b9e\u73b0StringBuffer\u7684\u62fc\u63a5\uff0c\u800c\u4e0d\u662f\u4e00\u5f00\u59cb\u5f00\u8f9f\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u7ec4\n    \/\/\u7ed3\u6784\u4f53StringBuffer \n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/memcpy \u7531src\u6240\u6307\u5185\u5b58\u533a\u57df\u590d\u5236count\u4e2a\u5b57\u8282\u5230dest\u6240\u6307\u5185\u5b58\u533a\u57df\nvoid main(void) {\n    char src[] = &quot;C,C++,Java&quot;;\n    char dest[20] = {0};\n\n    \/\/\u5b57\u8282\n    memcpy(dest, src, 5);\n\n    printf(&quot;%s\\n&quot;, dest);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/memchr \u4ecebuf\u6240\u6307\u5185\u5b58\u533a\u57df\u7684\u524dcount\u4e2a\u5b57\u8282\u67e5\u627e\u5b57\u7b26ch\u3002\n\/\/void *memchr(const void *buf, int ch, size_t count)\n\/\/\u529f\u80fd\uff1a\u4ecebuf\u6240\u6307\u5185\u5b58\u533a\u57df\u7684\u524dcount\u4e2a\u5b57\u8282\u67e5\u627e\u5b57\u7b26ch\u3002\nvoid main(void) {\n    char src[] = &quot;C,C++,Java&quot;;\n    char ch = &#039;C&#039;;\n\n    \/\/\u5b57\u8282 (\u5206\u6bb5\u622a\u53d6)\n    char* p = memchr(src+3, ch, 5);\n    if (p) {\n        printf(&quot;\u7d22\u5f15\uff1a%d\\n&quot;, p - src);\n    } else {\n        printf(&quot;\u6ca1\u6709\u627e\u5230\\n&quot;);\n    }\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/memmove \u7531src\u6240\u6307\u5185\u5b58\u533a\u57df\u590d\u5236count\u4e2a\u5b57\u8282\u5230dest\u6240\u6307\u5185\u5b58\u533a\u57df\u3002\n\/\/void *memmove(void* dest, const void* src, size_t count) \/\/\u7531src\u6240\u6307\u5185\u5b58\u533a\u57df\u590d\u5236count\u4e2a\u5b57\u8282\u5230dest\u6240\u6307\u5185\u5b58\u533a\u57df\nvoid main() {\n    char s[] = &quot;Michael Jackson!&quot;;\n    \/\/\u622a\u53d6\u7684\u6548\u679c    \n    memmove(s, s+8, strlen(s)-9);\n    s[strlen(s)-8] = 0;\n    printf(&quot;%s\\n&quot;, s);\n    getchar();\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u5728\u5b57\u7b26\u4e32s1\u4e2d\u5bfb\u627e\u5b57\u7b26\u4e32s2\u4e2d\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26\u76f8\u5339\u914d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e\uff0c\u7a7a\u5b57\u7b26NULL\u4e0d\u5305\u62ec\u5728\u5185\n\/\/char *strpbrk(char *str1, char *str2) \/\/\u53c2\u6570\u8bf4\u660e\uff1astr1\u5f85\u6bd4\u8f83\u7684\u5b57\u7b26\u4e32\uff0cstr2\u4e3a\u6307\u5b9a\u88ab\u641c\u7d22\u7684\u5b57\u7b26\u4e32\u3002\nvoid main() {\n    char *s1 = &quot;Welcome To Beijing&quot;;\n    char *s2 = &quot;to&quot;; \n    char *p;\n\n    p = strpbrk(s1, s2);\n    if (p)\n        printf(&quot;%s\\n&quot;, p);\n    else\n        printf(&quot;Not Found!\\n&quot;);\n\n    p = strpbrk(s1, &quot;Da&quot;);\n    if (p)\n        printf(&quot;%s&quot;, p);\n    else\n        printf(&quot;Not Found!&quot;);\n\n    getchar();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u7b26\u4e32 \/\/\u4f7f\u7528\u5b57\u7b26\u6570\u7ec4\u5b58\u50a8\u5b57\u7b26\u4e32 void main() { \/\/char str[] = {&#039;c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-405","post","type-post","status-publish","format-standard","hentry","category-android-ndk"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=405"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}