C/C++ 中 strchr() 函式


strchr() 函式用於在字串中搜索字元。它搜尋傳給其作為第二個引數的字元的第一次出現,若查詢成功則返回指向該字元的指標,否則返回 NULL。

以下是 C 語言中 strchr() 的語法形式:

char *strchr(const char *string , int character)

這裡,

string - 字串,需要掃描它以搜尋字元。

character - 字元,它是要在字串中搜索的字元。

以下是 C 語言中 strchr() 的示例:

示例

 執行演示

#include<stdio.h>
#include<string.h>
int main() {
   char s[] = "Helloworld!";
   char c = 'o';
   char *result = strchr(s, c);
   printf("String after searching the character : %s", result);
   return 0;
}

輸出

String after searching the character : oworld!

在上文中,傳遞了一個 char 型別的字串和在一個字串中需要搜尋的字元。結果儲存在指標變數 *result 中。

char s[] = "Helloworld!";
char c = 'o';
char *result = strchr(s, c);

更新於: 2020 年 6 月 26 日

313 次瀏覽

開始你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.