strchr() — 文字の検索
フォーマット
#include <string.h>
char *strchr(const char *string, int c);
言語レベル
ANSI
スレッド・セーフ
はい
ロケール依存
この関数の振る舞いは、現行ロケールの LC_CTYPE カテゴリーの影響を受ける可能性があります。詳細については、CCSID およびロケールの理解を参照してください。
説明
strchr() 関数は、ストリング内の文字の最初の出現を検出します。 文字 c は、ヌル文字 (¥0) にすることができます。 string の終了ヌル文字も検索に含まれます。
strchr() 関数は、ヌル終了ストリング上で作動します。関数のストリング引数には、 ストリングの終わりを示すマークであるヌル文字 (\0) が含まれていなければなりません。
戻り値
strchr() 関数は、string 内の文字に変換された c の最初の出現を指すポインターを戻します。 この関数は、指定された文字が見つからない場合、NULL を戻します。
例
この例では、"computer program" 内の文字 "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'
*/