C-standard-library-c-function-strspn

提供:Dev Guides
移動先:案内検索

Cライブラリ関数-strspn()

説明

Cライブラリ関数 size_t strspn(const char str1、const char * str2)は、 *str2 の文字のみで構成される str1 の初期セグメントの長さを計算します。

宣言

次に、strspn()関数の宣言を示します。

size_t strspn(const char *str1, const char *str2)

パラメーター

  • str1 -これはスキャンされるメインのC文字列です。
  • str2 -これは、str1で一致する文字のリストを含む文字列です。

戻り値

この関数は、str2の文字のみで構成されるstr1の初期セグメントの文字数を返します。

次の例は、strspn()関数の使用法を示しています。

#include <stdio.h>
#include <string.h>

int main () {
   int len;
   const char str1[] = "ABCDEFG019874";
   const char str2[] = "ABCD";

   len = strspn(str1, str2);

   printf("Length of initial segment matching %d\n", len );

   return(0);
}

次の結果を生成する上記のプログラムをコンパイルして実行しましょう-

Length of initial segment matching 4