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);
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP