<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C - 字符串和字符串函數 > 原文: [https://beginnersbook.com/2014/01/c-strings-string-functions/](https://beginnersbook.com/2014/01/c-strings-string-functions/) **字符串是一個字符數組**。在本指南中,我們將學習如何聲明字符串,如何在 C 編程中使用字符串以及如何使用預定義的字符串處理函數。 我們將看到如何比較兩個字符串,連接字符串,將一個字符串復制到另一個字符串,以及執行各種字符串操作。我們可以使用`string.h`頭文件的預定義函數執行此類操作。要使用這些字符串函數,必須在 C 程序中包含`string.h`文件。 ## 字符串聲明 ![string declaration in C](https://img.kancloud.cn/0b/db/0bdb93d1f4668d03a16e8b05429c4297_500x239.jpg) **方法 1:** ```c char address[]={'T', 'E', 'X', 'A', 'S', '\0'}; ``` **方法 2:上面的字符串也可以定義為:** ```c char address[]="TEXAS"; ``` 在上面的聲明中,空字符(`\0`)將自動插入字符串的末尾。 **什么是空字符`\0`?** `'\0'`表示字符串的結尾。它也被稱為字符串結尾或空字符。 ## C 編程中的字符串 I/O ![C String-IO](https://img.kancloud.cn/36/ca/36ca861ae44327a3881c844380f4904c_500x239.jpg) ### 使用`printf()`和`scanf()`函數在 C 中讀取和編寫字符串 ```c #include <stdio.h> #include <string.h> int main() { /* String Declaration*/ char nickname[20]; printf("Enter your Nick name:"); /* I am reading the input string and storing it in nickname * Array name alone works as a base address of array so * we can use nickname instead of &nickname here */ scanf("%s", nickname); /*Displaying String*/ printf("%s",nickname); return 0; } ``` 輸出: ```c Enter your Nick name:Negan Negan ``` **注意:**`%s`格式說明符用于字符串輸入/輸出 ### 使用`gets()`和`puts()`函數在 C 中讀取和編寫字符串 ```c #include <stdio.h> #include <string.h> int main() { /* String Declaration*/ char nickname[20]; /* Console display using puts */ puts("Enter your Nick name:"); /*Input using gets*/ gets(nickname); puts(nickname); return 0; } ``` ## C - 字符串函數 ![C string-functions](https://img.kancloud.cn/57/72/577272ed3f338cd0a44a3979a647d6ab_571x529.jpg) ### C 字符串函數 - `strlen` 語法: ```c size_t strlen(const char *str) ``` **`size_t`** 表示無符號短整數,它返回字符串的長度而不包括結束字符**(終止字符`\0`)**。 **`strlen`的例子:** ```c #include <stdio.h> #include <string.h> int main() { char str1[20] = "BeginnersBook"; printf("Length of string str1: %d", strlen(str1)); return 0; } ``` 輸出: ```c Length of string str1: 13 ``` **`strlen` vs `sizeof`** `strlen`返回存儲在數組中的字符串的長度,但`sizeof`返回分配給數組的總大小。因此,如果我再次考慮上述示例,則以下語句將返回以下值。 `strlen(str1)`返回值 13。`sizeof(str1)`將返回值 20,因為數組大小為 20(請參閱`main`函數中的第一個語句)。 ### C 字符串函數 - `strnlen` 語法: ```c size_t strnlen(const char *str, size_t maxlen) ``` `size_t`表示無符號短整數。如果字符串小于為`maxlen`指定的值(最大長度),則返回字符串的長度,否則返回`maxlen`值。 **`strnlen`的例子:** ```c #include <stdio.h> #include <string.h> int main() { char str1[20] = "BeginnersBook"; printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30)); printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10)); return 0; } ``` 輸出: ```c Length of string str1 when maxlen is 30: 13 Length of string str1 when maxlen is 10: 10 ``` 您是否注意到第二個`printf`語句的輸出,即使字符串長度為 13,它只返回 10,因為`maxlen`為 10。 ### C 字符串函數 - `strcmp` ```c int strcmp(const char *str1, const char *str2) ``` 它比較兩個字符串并返回一個整數值。如果兩個字符串相同(相等),則此函數將返回 0,否則它可能會根據比較返回負值或正值。 **如果`string1<string2`或者`string1`是`string2`**的子字符串,它會產生負值。如果`string1>string2`它將返回正值。 **如果`string1 == string2`** ,那么當你將此函數用于比較字符串時,你會得到 0。 **`strcmp`示例:** ```c #include <stdio.h> #include <string.h> int main() { char s1[20] = "BeginnersBook"; char s2[20] = "BeginnersBook.COM"; if (strcmp(s1, s2) ==0) { printf("string 1 and string 2 are equal"); }else { printf("string 1 and 2 are different"); } return 0; } ``` **輸出:** ```c string 1 and 2 are different ``` ### C 字符串函數 - `strncmp` ```c int strncmp(const char *str1, const char *str2, size_t n) ``` `size_t`用于無符號短整數。它比較字符串直到`n`個字符,或者換句話說,它比較兩個字符串的前`n`個字符。 **`strncmp`示例:** ```c #include <stdio.h> #include <string.h> int main() { char s1[20] = "BeginnersBook"; char s2[20] = "BeginnersBook.COM"; /* below it is comparing first 8 characters of s1 and s2*/ if (strncmp(s1, s2, 8) ==0) { printf("string 1 and string 2 are equal"); }else { printf("string 1 and 2 are different"); } return 0; } ``` **輸出:** ```c string1 and string 2 are equal ``` ### C 字符串函數 - `strcat` ```c char *strcat(char *str1, char *str2) ``` 它連接兩個字符串并返回連接的字符串。 **`strcat`示例:** ```c #include <stdio.h> #include <string.h> int main() { char s1[10] = "Hello"; char s2[10] = "World"; strcat(s1,s2); printf("Output string after concatenation: %s", s1); return 0; } ``` **輸出:** ```c Output string after concatenation: HelloWorld ``` ### C 字符串函數 - `strncat` ```c char *strncat(char *str1, char *str2, int n) ``` 它將`str2`的`n`個字符連接到字符串`str1`。終結符(`\0`)將始終附加在連接字符串的末尾。 **`strncat`示例:** ```c #include <stdio.h> #include <string.h> int main() { char s1[10] = "Hello"; char s2[10] = "World"; strncat(s1,s2, 3); printf("Concatenation using strncat: %s", s1); return 0; } ``` **輸出:** ```c Concatenation using strncat: HelloWor ``` ### C 字符串函數 - `strcpy` ```c char *strcpy( char *str1, char *str2) ``` 它將字符串`str2`復制到字符串`str1`中,包括結束字符(終結符`\0`)。 **`strcpy`示例:** ```c #include <stdio.h> #include <string.h> int main() { char s1[30] = "string 1"; char s2[30] = "string 2 : I’m gonna copied into s1"; /* this function has copied s2 into s1*/ strcpy(s1,s2); printf("String s1 is: %s", s1); return 0; } ``` **輸出:** ```c String s1 is: string 2: I’m gonna copied into s1 ``` ### C 字符串函數 - `strncpy` ```c char *strncpy(char *str1, char *str2, size_t n) ``` `size_t`是無符號`short`,`n`是數字。 **情況 1:**如果`str2`的長度`>n`然后它只是將`str2`的前`n`個字符復制到`str1`中。 **情況 2:**如果`str2`的長度`<n`。然后它將`str2`的所有字符復制到`str1`中,并附加幾個終結符字符(`\0`)以填充`str1`的長度使其成為`n`。 **`strncpy`的例子:** ```c #include <stdio.h> #include <string.h> int main() { char first[30] = "string 1"; char second[30] = "string 2: I’m using strncpy now"; /* this function has copied first 10 chars of s2 into s1*/ strncpy(s1,s2, 12); printf("String s1 is: %s", s1); return 0; } ``` **輸出:** ```c String s1 is: string 2: I’m ``` ### C 字符串函數 - `strchr` ```c char *strchr(char *str, int ch) ``` 它在字符串`str`中搜索字符`ch`(您可能想知道在上面的定義中我已經將`ch`的數據類型賦予了`int`,不要擔心我沒有犯任何錯誤它應該只是`int`。事情是當我們將任何值給`strchr`時,它會在內部轉換為整數以便更好地搜索。 **`strchr`的例子:** ```c #include <stdio.h> #include <string.h> int main() { char mystr[30] = "I’m an example of function strchr"; printf ("%s", strchr(mystr, 'f')); return 0; } ``` **輸出:** ```c f function strchr ``` ### C 字符串函數 - `strrchr` ```c char *strrchr(char *str, int ch) ``` 它類似于`strchr`函數,唯一的區別是它以相反的順序搜索字符串,現在你已經理解為什么我們在`strrchr`中有額外的`r`,是的你猜對了它,它只是反向的。 現在讓我們采用相同的上述示例: ```c #include <stdio.h> #include <string.h> int main() { char mystr[30] = "I’m an example of function strchr"; printf ("%s", strrchr(mystr, 'f')); return 0; } ``` **輸出:** ```c function strchr ``` **為什么輸出與`strchr`不同?** 這是因為它從字符串的末尾開始搜索并在函數中找到第一個`'f'`而不是`'of'`。 ### C 字符串函數 - `strstr` ```c char *strstr(char *str, char *srch_term) ``` 它類似于`strchr`,除了它搜索字符串`srch_term`而不是單個字符。 **`strstr`示例:** ```c #include <stdio.h> #include <string.h> int main() { char inputstr[70] = "String Function in C at BeginnersBook.COM"; printf ("Output string is: %s", strstr(inputstr, 'Begi')); return 0; } ``` **輸出:** ```c Output string is: BeginnersBook.COM ``` 您也可以使用此函數代替`strchr`,因為您也可以使用單個字符代替`search_term`字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看