strchr ()- 搜尋字元
格式
#include <string.h>
char *strchr(const char *string, int c);語言層次
ANSI
安全執行緒
是
區分語言環境
此函數的行為可能受現行語言環境的 LC_CTYPE 種類影響。 如需相關資訊,請參閱 瞭解 CCSID 及語言環境。
說明
strchr() 函數會尋找字串中第一個出現的字元。 字元 c 可以是空值字元 (\0); 搜尋中包括 string 的結尾空值字元。
strchr() 函數會對以空值結尾的字串進行操作。 函數的字串引數應該包含標示字串結尾的空值字元 (\0)。
回覆值
strchr() 函數會傳回第一次出現的 c (轉換為 string中的字元) 的指標。 如果找不到指定的字元,則函數會傳回 NULL 。
範例
此範例會尋找
電腦程式中第一個出現的字元
p。
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char buffer1[SIZE] = "computer program";
char * ptr;
int ch = 'p';
ptr = strchr( buffer1, ch );
printf( "The first occurrence of %c in '%s' is '%s'\n",
ch, buffer1, ptr );
}
/***************** Output should be similar to: *****************
The first occurrence of p in 'computer program' is 'puter program'
*/