strchr関数は文字列内から指定した文字の位置のポインタ値を返却します。
以下にCサンプルコードと実行例を記します。
strchrの書式は以下のようになります。 strchrを使用するには、string.hのインクルードが必要です。
#include <string.h>
書式は以下のようになります。
char *strchr(const char *s, int c);
つまり
&ref(): File not found: "strchr.c" at page "文字列/指定した文字の場所を取得する・strchr"; (改行コードLF)
#include <stdio.h> #include <string.h> int main(void) { char *str = "abcdefghijklmnopqrstuvwxyz"; char *p; p = strchr(str, 'c'); if (p != NULL) { printf("find(c) : %s\n", p); } else { printf("find c is not found.\n"); } p = strchr(str, 'x'); if (p != NULL) { printf("find(x) : %s\n", p); } else { printf("find x is not found.\n"); } p = strchr(str, 'A'); if (p != NULL) { printf("find(A) : %s\n", p); } else { printf("find A is not found.\n"); } return 0; }
実際にコンパイルし実行した時の出力結果です。
指定した文字から表示されているのが確認できます。
また対応する文字がない場合はNULLが返却されているのが確認できます。
$ gcc strchr.c -o strchr $ ./strchr find(c) : cdefghijklmnopqrstuvwxyz find(x) : xyz find A is not found.
以上、strchrのCサンプルコードでした。
c-btm.html is not found or not readable.