strchr() - 文字の検索

標準

標準/拡張機能 C/C++ 依存項目

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3

両方  

形式

C:
#include <string.h>

char *strchr(const char *string, int c);
C++:
#include <string.h>

const char *strchr(const char *string, int c);
      char *strchr(char *string, int c);

機能説明

strchr() 組み込み関数によって、char に変換された c の最初の出現が、ストリング *string で検出されます。文字 c を、NULL 文字 (¥0) にする ことができます。string の終了 NULL 文字は、検索に組み込まれます。

strchr() 関数は、NULL 文字で終了するストリングを操作します。関数のストリング引数には、ストリングの終わりにマークを付ける NULL 文字 (¥0) が 含まれていることが必要です

戻り値

正常に実行された場合、strchr() は、(文字に変換された) c の最初の出現のポインターを string で戻します。

文字が見つからない場合には、strchr() により NULL ポインターが戻されます。

CELEBS35
/* CELEBS35                                      

   This example finds the first occurrence of the character p in                
   "computer program".                                                          
                                                                                
 */                                                                             
#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 );                                                 
                                                                                
}                                                                               
出力:
The first occurrence of p in 'computer program' is 'puter program'