- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - strcspn() 函式
C 庫的 strcspn() 函式接受兩個指標變數作為引數,計算初始段(str1)的長度,並且該段完全由不在 str2 中的字元組成。
通常,它用於查詢給定字串的長度,並返回從開頭開始的字元數。
語法
以下是 C 庫 strcspn() 函式的語法:
size_t strcspn(const char *str1, const char *str2)
引數
此函式接受以下引數:
str1 - 這是要掃描的主 C 字串。
str2 - 這是一個包含與 str1 匹配的字元列表的字串。
返回值
此函式返回字串 str1 的初始段的長度,該段不包含字串 str2 中的任何字元。
示例 1
以下 C 庫程式說明了 strcspn() 函式如何檢查字串中的第一個不匹配字元。
#include <stdio.h>
#include <string.h>
int main () {
int len;
// Intializing string(Unmatched Characters)
const char str1[] = "Tutorialspoint";
const char str2[] = "Textbook";
len = strcspn(str1, str2);
printf("First matched character is at %d\n", len + 1);
return(0);
}
輸出
以上程式碼產生以下結果:
First matched character is at 10
示例 2
我們使用 strcspn() 方法來顯示匹配的字元。
#include <stdio.h>
#include <string.h>
int main() {
int size;
// Intializing string(Matched Characters)
char str1[] = "tutorialspoint";
char str2[] = "tutorial";
// Using strcspn() to
size = strcspn(str1, str2);
printf("The unmatched characters before the first matched character: %d\n", size);
return 0;
}
輸出
以上程式碼產生以下結果:
The unmatched characters before the first matched character: 0
示例 3
這裡,我們使用 strcspn() 函式確定不包含給定集合中任何字元的初始段的長度。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Welcome to Tutorialspoint Community";
char str2[] = "point";
size_t len = strcspn(str1, str2);
// Display the output
printf("The length of the initial segment of str1 that does not contain any characters from str2 is: %zu\n", len);
return 0;
}
輸出
以上程式碼產生以下結果:
The length of the initial segment of str1 that does not contain any characters from str2 is: 4
廣告