工具標籤
C 標準函式庫
C 標準庫 - 參考手冊
C 標準庫 - <assert.h>
C 標準函式庫 - <ctype.h>
C 標準函式庫 - <errno.h>
C 標準函式庫 - <float.h>
C 標準庫 - <limits.h>
C 標準函式庫 - <locale.h>
C 標準函式庫 - <math.h>
C 標準函式庫 - <setjmp.h>
C 標準函式庫 - <signal.h>
C 標準函式庫 - <stdarg.h>
C 標準函式庫 - <stddef.h>
C 標準函式庫 - <stdio.h>
C 標準函式庫 - <stdlib.h>
C 標準函式庫 - <string.h>
C 標準函式庫 - <time.h>
目錄
工具標籤
C 標準函式庫
C 標準庫 - 參考手冊
C 標準庫 - <assert.h>
C 標準函式庫 - <ctype.h>
C 標準函式庫 - <errno.h>
C 標準函式庫 - <float.h>
C 標準庫 - <limits.h>
C 標準函式庫 - <locale.h>
C 標準函式庫 - <math.h>
C 標準函式庫 - <setjmp.h>
C 標準函式庫 - <signal.h>
C 標準函式庫 - <stdarg.h>
C 標準函式庫 - <stddef.h>
C 標準函式庫 - <stdio.h>
C 標準函式庫 - <stdlib.h>
C 標準函式庫 - <string.h>
C 標準函式庫 - <time.h>
C 字串
在 C 語言中,字串實際上是使用 null 字元 '\0' 終止的一維字元陣列。因此,以 null 結尾的字串,包含了組成字串的字元。
下面的宣告和初始化創建了一個 "Hello" 字串。由於在數組的末尾存儲了空字符,所以字符數組的大小比單字 "Hello" 的字符數多一個。
char greeting[6] = {'H', 'e', 'l', 'l', 'o', 'char greeting[] = "Hello";
'};依據陣列初始化規則,您可以把上面的語句寫成以下語句:
#include <stdio.h>int main (){ char greeting[6] = {'H', 'e', 'l', 'l', 'o', 'Greeting message: Hello
'};
printf("Greeting message: %s\n", greeting ); return 0;}以下是C/C++ 中定義的字串的記憶體表示:

#其實,您不需要把null 字元放在字串常數的末尾。 C 編譯器會在初始化陣列時,自動把 '\0' 放在字串的最後。讓我們試著輸出上面的字串:
#include <stdio.h>#include <string.h>int main (){ char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; /* 复制 str1 到 str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 ); /* 连接 str1 和 str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 ); /* 连接后,str1 的总长度 */
len = strlen(str1);
printf("strlen(str1) : %d\n", len ); return 0;}當上面的程式碼被編譯和執行時,它會產生下列結果:
strcpy( str3, str1) : Hellostrcat( str1, str2): HelloWorldstrlen(str1) : 10
C 中有大量操作字串的函數:
| 序號 | 函數& 目的 |
|---|---|
| #1 | strcpy(s1, s2); 複製字串s2 到字串s1。 |
| 2 | strcat(s1, s2); 連接字串 s2 到字串 s1 的結尾。 |
| 3 | strlen(s1); 傳回字串 s1 的長度。 |
| 4 | strcmp(s1, s2); 如果s1 和s2 是相同的,則回傳0;如果s1< s2 則回傳小於0;如果s1>s2 則回傳大於0。 |
| 5 | strchr(s1, ch); 傳回一個指針,指向字串s1 中字元ch 的第一次出現的位置。 |
| 6 | strstr(s1, s2); 傳回一個指針,指向字串s1 中字串s2 的第一次出現的位置。 |
下面的實例使用了上述的一些函數:
rrreee當上面的程式碼被編譯和執行時,它會產生下列結果:
rrreee您可以在C 標準庫中找到更多字串相關的函數。
相關影片
熱AI工具
Undress AI Tool
免費脫衣圖片
AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片
Stock Market GPT
人工智慧支援投資研究,做出更明智的決策
熱門文章
如何在Golang中應用外觀模式(Facade) Go語言簡化複雜系統的API
1 個月前 By DDD
如何在 Windows 11 中管理和停止自動 BitLocker 加密 - 讓技術更簡單
1 個月前 By Jack chen
Pokemon FireRed 和 LeafGreen TM 位置
3 週前 By DDD
如何在Golang中解析和產生CSV檔案 Go語言encoding/csv標準函式庫技巧
1 個月前 By DDD
Python zip函數怎麼用_並行遍歷多個序列與字典構建
3 週前 By DDD
熱門話題
# 抖音等級價目表1-75
20524
7
20524
7
# wifi顯示無ip分配
13636
4
13636
4
# 虛擬手機號碼接收驗證碼
11968
4
11968
4
# gmail信箱登陸入口在哪裡
9020
17
9020
17
# windows安全中心怎麼關閉
8508
7
8508
7
熱門工具
記事本++7.3.1
好用且免費的程式碼編輯器
SublimeText3漢化版
中文版,非常好用
禪工作室 13.0.1
強大的PHP整合開發環境
Dreamweaver CS6
視覺化網頁開發工具
SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)












![PHP實戰開發極速入門: PHP快速創建[小型商業論壇]](https://img.php.cn/upload/course/000/000/035/5d27fb58823dc974.jpg)
