C 語言字符串處理函數

字符串是一種重要的數據類型,編寫的任何應用程序,只要使用文本數據、或者與用戶進行交互,都離不開對字符串的處理。

C 語言提供了一組字符串操作函數,方便開發者對字符串進行操作,函數原型在標準函數庫 <string.h> 文件內。

獲取字符串的長度

用於獲取字符串的長度,即字符串包含的有效字符個數(不包含結束符‘\0’),函數原型爲:

unsigned int strlen(char *s);

函數返回字符數組 s 包含的有效字符個數,s 爲指向字符數組的指針。

返回值:

字符串包含的有效字符個數。

下面的案例代碼展示了 strlen() 函數的使用。

include <stdio.h>
// 包含strlen函數的頭文件
#include <string.h>
int main() {
// 示例字符串
char exampleString[] = "Hello, World!";
// 使用strlen函數獲取字符串長度
size_t length = strlen(exampleString);
// 打印字符串長度
printf("The length of the string \"%s\" is: %d\n", exampleString, length);
return 0;
}

在案例代碼中,strlen 函數被用來計算 exampleString 的長度,並將結果存儲在 length 變量中。然後使用 printf 函數輸出字符串和它的長度。注意,strlen 返回的是 size_t 類型的值,這是一個無符號整數類型,通常用於表示對象的大小。

字符串的比較

用於比較兩個字符串並根據比較結果返回一個整數,函數原型爲:

int strcmp (const char * str1, const char * str2);

函數開始比較 str1 和 str2 的第一個字符,如果它們彼此相等,則繼續比較後續字符,直到字符不同或到達 str1 或 str2 的結束符。

返回值:

如果 s1 和 s2 相同,則返回 0。

如果 s1 在字典序上小於 s2,則返回一個負數。

如果 s1 在字典序上大於 s2,則返回一個正數。

下面的案例代碼展示了 strcmp() 函數的使用。

#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
char str3[] = "Hello";
int result1 = strcmp(str1, str2);
int result2 = strcmp(str1, str3);
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str2, result1);
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str3, result2);
return 0;
}

程序運行後輸出結果:

strcmp("Hello", "World") = -1

strcmp("Hello", "Hello") = 0

strcmp 函數注意事項

strcmp 函數對大小寫敏感,因此 "Hello" 和 "hello" 是不同的。函數比較的是字符串的內容,而不是字符串的指針或地址。如果 s1 和 s2 中有一個是空指針(NULL),則 strcmp 函數的行爲是未定義的,通常會導致程序崩潰。strcmp 函數按照字符的 ASCII 值進行比較。

查找子串

用於查找字符串是否包含指定的子串,子串就是字符串中一組任意連續的字符。函數原型爲:

char * strstr (char * str1, const char * str2);

函數返回指向 str1 中第一個出現的 str2 的指針,如果 str2 不是 str1 的一部分,則返回空指針(即指針值爲 0)。

下面的案例代碼展示了 strstr() 函數的使用。

#include <stdio.h>
#include <string.h>
int main() {
const char *str1 = "Hello, world!";
const char *str2 = "world";
char *result;
result = strstr(str1, str2);
if (result != NULL) {
printf("Found '%s' in '%s' at position: %ld\n", str2, str1, result - str1);
} else {
printf("'%s' not found in '%s'\n", str2, str1);
}
return 0;
}

案例代碼會在 str1 中查找 str2,並打印出 str2 在 str1 中首次出現的位置(從 str1 的開始位置計算)。

strstr 函數注意事項

Strstr() 函數對大小寫敏感。例如:"Hello, World!" 中找不到 "world",因爲大小寫不匹配。strstr 返回的是指向 str1 中字符串的指針,而不是一個新的字符串,返回的指針實際上是指向 str1 的內存地址。

字符串的連接

用於連接兩個字符串,兩個字符串合併爲一個字符串,函數原型爲:

char * strcat (char * destination, const char * source);

函數將 source 的副本附加到 destination 字符串的尾部 ,destination 的結束符被 source 的第一個字符覆蓋。函數返回 destination。

下面的案例代碼展示了 strcat() 函數的使用。

#include <stdio.h>
#include <string.h>
int main() {
char dest[50] = "Hello, ";
const char *src = "World!";
strcat(dest, src);
printf("%s\n", dest); // 輸出:Hello, World!
return 0;
}

使用 strcat() 函數連接字符串時,需要注意緩衝區是否溢出,strcat 不會自動檢查目標字符串是否有足夠的空間來容納源字符串。如果目標字符串的空間不足以容納兩個字符串的總和,就會發生緩衝區溢出。

字符串分割

用於分解字符串,它主要用於將一個字符串按照指定的分隔符進行分割,生成一系列的子字符串。

函數原型爲:

char *strtok(char *str, const char *delim);

str:要進行分解的字符串。第一次調用 strtok 時,str 應該是原始字符串。之後的調用,str 應該是 NULL,因爲 strtok 會在內部保存其狀態,以便進行連續的分解操作。

delim:指定分隔符的字符串,strtok 會使用 delim 來分割原始字符串。

strtok 函數會修改原始字符串,通過插入空字符(\0)來分隔子字符串。因此在分解原始字符串之前,最好將原始字符串複製一份。

下面的案例代碼展示了 strcat() 函數的使用。

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "This,is,a,test";
char *token;
/* 獲取第一個子字符串 */
token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
/* 獲取下一個子字符串 */
token = strtok(NULL, ",");
}
return 0;
}

程序運行結果爲:

This
is
a
test
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/Q5qnzstbjyAYM-1g8Awv3Q