C語言常用的字符串函數總結


一、strlen

1、原型:size_t strlen(char const* string);

2、功能:返回字符串 string 的長度(不包含字符串終止符NUL)

3、注意:size_t是一個無符號整數類型

4、舉例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main()
 6 {
 7     char* y = "abcdef";
 8     char* x = "abcd";
 9 
10     if (strlen(x) >= strlen(y)) {
11         printf("1: strlen(x) >= strlen(y)\n");
12     } else {
13         printf("1: strlen(x) < strlen(y)\n");
14     }
15 
16     /* 由於strlen(x)返回的是一個size_t,所以strlen(x) - strlen(y) >= 0恆成立,
17      * 導致出現錯誤的結論
18      */
19     if (strlen(x) - strlen(y) >= 0) { 
20         printf("2: strlen(x) - strlen(y) >= 0\n");
21     } else {
22         printf("2: strlen(x) - strlen(y) < 0\n");
23     }
24     
25     // 將size_t轉換為int類型后,可以返回正確的值
26     if ((int)(strlen(x)) - (int)(strlen(y)) >= 0) { 
27         printf("3: (int)strlen(x) - strlen(y) >= 0\n");
28     } else {
29         printf("3: (int)strlen(x) - strlen(y) < 0\n");
30     }
31     
32     return 0;
33 }
View Code

運行結果:

 

二、strcpy

1、原型:char *strcpy(char *dst, char const *src);

2、功能:將參數src字符串復制到dst參數中。如果參數src和dst在內存中出現重疊,其結果是未定義的。由於dst參數將進行修改,所以它必須是個字符數組或者是一個指向動態分配內存的數組的指針,不能使用字符串常量。返回參數dst的一份拷貝。

3、注意:

目標參數dst的以前內容將被覆蓋並丟失。即使新的字符串比dst原先的內存更短,由於新字符串是以NUL字符結尾,所以老字符串最后剩余的幾個字符也會被有效的刪除。如果字符串比數組長,多余的字符仍被復制,它們將覆蓋原先存儲於數組后面的內存空間的值。所以必須保證目標字符數組的空間足以容納需要復制的字符串。

4、舉例

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main()
 6 {
 7     char msg[] = "Original message";
 8     printf("before strcpy: msg:%s\n", msg);
 9     strcpy(msg, "Different");
10     printf("after strcpy: msg:%s\n", msg);
11     return 0;
12 }
View Code

運行結果:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main()
 6 {
 7     char msg[] = "Different";
 8     printf("before strcpy: msg:%s\n", msg);
 9     strcpy(msg, "Original message");
10     printf("after strcpy: msg:%s\n",msg);
11     return 0;
12 }
View Code

運行結果:

 

 

 

 

 三、strncpy

1、原型:char *strncpy(char *dst, char const *src, size_t len);

2、功能:和strcpy一樣,strncpy把源字符串的字符復制到目標數組。然而,它總是 正好向dst寫入len個字符。如果strlen(src)的值小於len, dst數組就用額外的NUL字節填充到len長度。如果strlen(src)的值大於或者等於len,那么只有len個字符被復制到dst中。

3、注意:strncpy調用的結果可能不是一個字符串,它的結果將不會以NUL字符結尾, 因此字符串必須以NUL字符結尾

4、舉例

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #define STR_CPY_LEN 5
 6 int main()
 7 {
 8     char msg[] = "Original message";
 9     printf("before strncpy: msg:%s\n", msg);
10     strncpy(msg, "Different", STR_CPY_LEN);
11     printf("after strncpy: msg:%s\n",msg);
12     return 0;
13 }
View Code

運行結果:

 

四、strcat

1、原型:char *strcat(char *dst, char const *src);

2、功能:將一個字符串添加(連接)到另一個字符串的后面。

3、注意:src和dst的所指的內存區域不能重疊,如果發生重疊,其結果是未定義的。

4、舉例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main()
 6 {
 7     char msg[20] = "hello";
 8     printf("before strcat: msg:%s\n", msg);
 9     strcat(msg, ", world.");
10     printf("after strcat: msg:%s\n", msg);
11     return 0;
12 }
View Code

運行結果:

 

五、strncat

1、原型:char *strncat(char *dst, char const *src, size_t len);

2、功能:它從src最多復制 len個字符到dst中。但是, strncat總是在結果字符串后面添加一個NUL字符。

3、注意:src和dst所指的內存區域不能重疊,並且dst必須有足夠多的空間來容納src的字符串。

4、舉例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define LEN 5
 5 int main()
 6 {
 7     char msg[20] = "hello";
 8     printf("before strcat: len: %d, msg:%s\n", strlen(msg), msg);
 9     strncat(msg, ", world.", LEN);
10     printf("after strcat: len: %d, msg:%s\n", strlen(msg), msg);
11     return 0;
12 }
View Code

運行結果:

 

六、strcmp

1、原型:int strcmp(char const *s1, char const *s2);

2、功能:比較兩個字符串。如果s1小於s2,strcmp函數返回一個小於零的值。如果s1大於s2,函數返回一個大於零的值。如果兩個字符串相等,函數就返回零。

3、注意:由於strcmp並不修改它的任何一個參數,所以不存在溢出字符數組的危險。但是,和其他不受限制的字符串函數(strcpy, strcat)一樣,strcmp函數的字符串參數也必須以一個NUL字符結尾。如果並非如此,strcmp就可能對參數后面的字符進行比較,這個比較結果將不會有什么意義。

4、舉例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 
 6 int main()
 7 {
 8     char *s1 = "hello";
 9     char *s2 = "hello, world";
10     int ans = strcmp(s1, s2);
11     if (ans > 0) {
12         printf("s1 > s2\n");
13     } else if (ans < 0) {
14         printf("s1 < s2\n");
15     } else {
16         printf("s1 = s2\n");
17     }
18     return 0;
19 }
View Code

運行結果:

 

七、strncmp

1、原型:int strncmp(char const *s1, char const *s2, size_t len);

 2、功能:和strcmp一樣,也用於比較兩個字符串,但它最多比較 len 個字節。如果兩個字符串在第 len 個字符之前存在不相等的字符,這個函數就像strcmp一樣停止比較,返回結果。如果兩個字符串的前len 個字符相等,函數就返回零。

3、舉例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define LEN 5
 5 
 6 int main()
 7 {
 8     char *s1 = "hello";
 9     char *s2 = "hello, world";
10     int ans = strncmp(s1, s2, LEN);
11     if (ans > 0) {
12         printf("s1 > s2\n");
13     } else if (ans < 0) {
14         printf("s1 < s2\n");
15     } else {
16         printf("s1 = s2\n");
17     }
18     return 0;
19 }
View Code

返回結果:

 

八、strchr、strrchr

1、原型:char *strchr(char const *str, int ch);

      char *strrchr(char const *str, int ch);

2、功能:在一個字符串中查找一個特定字符。

3、注意:第2個參數是一個整型值。但是,它包含了一個字符值。strchr在字符串str中查找字符ch第一次出現的位置,找到后函數返回一個指向該位置的指針。如果該字符並不存在於str中,函數就返回一個NULL指針。strrchr的功能和strchr基本一致,只是它所返回的是一個指向字符串中該字符最后一次出現的位置(最右邊那個)。

4、舉例:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char string[20] = "Hello there, honey.";
 7     char *ans;
 8     char *ans1;
 9     ans = strchr(string, 'h');
10     printf("ans:|%s|\n", ans);
11     ans1 = strrchr(string, 'h');
12     printf("ans:|%s|\n\n", ans1);
13 
14     ans = strchr(string, 'a');
15     printf("ans:|%s|\n", ans);
16     ans1 = strrchr(string, 'a');
17     printf("ans:|%s|\n", ans1);
18     return 0;
19 }
View Code

運行結果:

 

九、strpbrk

1、原型:char *strpbrk(char const *str, char const *group);

2、功能:這個函數返回一個指向str中第1個匹配group中任何一個字符的字符位置。如果未找到匹配,函數返回一個NULL指針。

3、舉例:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char string[20] = "Hello there, honey.";
 7     char *ans;
 8     ans = strpbrk(string, "aeiou");
 9     printf("ans:|%s|\n", ans);
10     return 0;
11 }
View Code

運行結果:

 

 十、strstr

1、原型:char *strstr(char *s1, char *s2);

2、功能:這個函數在s1中查找整個s2第1次出現的起始位置,並返回一個指向該位置的指針。如果s2並沒有完整地出現在s1的任何地方,函數將返回一個NULL指針。如果第2個參數是一個空字符串,函數就返回s1。

3、舉例:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char s1[20] = "Hello there, honey.";
 7     char s2[] = "honey";
 8     char *s3 = "";
 9     char *ans;
10     ans = strstr(s1, s2);
11     printf("ans:|%s|\n", ans);
12 
13     ans = strstr(s1, s3);
14     printf("ans:|%s|\n", ans);
15     return 0;
16 }
View Code

運行代碼:

 

標准庫中並不存在strrstr或者strrpbrk函數。下面是其中的一個實現:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char *My_strrstr(char const *s1, char const *s2)
 5 {
 6     char *last = NULL;
 7     char *current = NULL;
 8 
 9     /* 只在第2個字符串不為空時才進行查找,如果s2為空,返回NULL */
10     if (*s2 != '\0') {
11         /* 查找s2在s1中第1次出現的位置 */
12         current = strstr(s1, s2);
13 
14         /* 我們每次找到字符串時,讓指針指向它的起始位置,然后查找該字符串下一個匹配位置 */
15         while (current != NULL) {
16             last = current;
17             current = strstr(last + 1, s2);
18         }
19     }
20     return last;
21 }
22 
23 char *My_strrpbrk(char const *s1, char const *group)
24 {
25     char *last = NULL;
26     char *current = NULL;
27 
28     /* 只在第2個字符串不為空時才進行查找,如果s2為空,返回NULL */
29     if (*group != '\0') {
30         /* 查找s2在s1中第1次出現的位置 */
31         current = strpbrk(s1, group);
32 
33         /* 我們每次找到字符串時,讓指針指向它的起始位置,然后查找該字符串下一個匹配位置 */
34         while (current != NULL) {
35             last = current;
36             current = strpbrk(last + 1, group);
37         }
38     }
39     return last;
40 }
41 
42 int main()
43 {
44     char s1[30] = "Hello there, honey.honey";
45     char s2[] = "honey";
46     char *s3 = "";
47     char *ans;
48     ans = strstr(s1, s2);
49     printf("ans:|%s|\n", ans);
50 
51     ans = strstr(s1, s3);
52     printf("ans:|%s|\n", ans);
53 
54     ans = My_strrstr(s1, s2);
55     printf("ans:|%s|\n", ans);
56 
57     ans = strpbrk(s1, "e");
58     printf("ans:|%s|\n", ans);
59 
60     ans = My_strrpbrk(s1, "e");
61     printf("ans:|%s|\n", ans);
62 
63     return 0;
64 }
View Code

運行結果:

 

十一、strtok

1、原型:char *strtok(char *str, char const *sep);

2、功能:分解字符串str為一組字符串,分隔符為sep。

3、注意:如果strtok函數的第1個參數不是NULL,函數將找到字符串的第1個標記。strtok同時將保存它在字符串中的位置。如果strtok函數的第1個參數是NULL,函數就在同一個字符串中從這個被保存的位置開始像前面一樣查找下一個標記。如果字符串內不存在更多的標記,strtok函數就返回一個NULL指針。在典型情況下,在第1次調用strtok時,向它傳遞一個指向字符串的指針。然后,這個函數被重復調用(第1個參數為NULL),直到它返回NULL為止。

4、舉例:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char whitespace[] = " ";
 7     char *token;
 8     char line[] = "I love you";
 9     for (token = strtok(line, whitespace); token !=NULL;
10          token = strtok(NULL, whitespace)) {
11              printf("Next token is |%s|\n", token);
12         }
13     return 0;
14 }
View Code

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM