- 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庫 - strpbrk() 函式
C庫的strpbrk()函式查詢字串str1中第一個與str2中指定的任何字元匹配的字元。這並不包括終止的空字元。例如,當用戶使用csv檔案時,它有助於定位逗號、分號或其他分隔值的字元。
語法
以下是C庫strpbrk()函式的語法:
char *strpbrk(const char *str1, const char *str2)
引數
此函式接受以下引數:
- str1 − 這是要掃描的C字串。
- str2 − 這是包含要匹配字元的C字串。
返回值
此函式返回指向str1中與str2中的一個字元匹配的字元的指標,如果找不到這樣的字元,則返回NULL。
示例1
以下是一個演示strpbrk()函式用法的基本C庫程式。
#include <stdio.h>
#include <string.h>
int main () {
const char str1[] = "abcde2fghi3jk4l";
const char str2[] = "34";
char *ret;
ret = strpbrk(str1, str2);
if(ret) {
printf("First matching character: %c\n", *ret);
}
return(0);
}
輸出
執行上述程式碼後,我們得到以下結果:
First matching character: 3
示例2
在這個例子中,我們將找到字串中一組字元的第一次出現。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
// Set of characters to search for
char set[] = "oW";
char *result = strpbrk(str, set);
if (result != NULL) {
printf("First occurrence found at index: %ld\n", result - str);
} else {
printf("No occurrence found.\n");
}
return 0;
}
輸出
執行程式碼後,我們得到以下結果:
First occurrence found at index: 4
示例3
我們建立一個C程式來替換字串中一組字元的所有出現。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Our Tutorials";
// Set of characters to search for
char set[] = "rT";
char *result = strpbrk(str, set);
if (result != NULL) {
printf("First occurrence found at index: %ld\n", result - str);
} else {
printf("No occurrence found.\n");
}
return 0;
}
輸出
上述程式碼產生以下結果:
First occurrence found at index: 2
廣告