文字列/指定した文字列が存在する位置を返却する・strstr
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* 指定した文字列が存在する位置を返却する・strstr [#v740a...
strstr関数は引数で指定した文字列が存在した場合、存在した...
以下にCサンプルコードと実行例を記します。
#contents
#htmlinsertpcsp(c-top.html,c-sp.html)
* 関連記事 [#u9fbbeba]
-[[指定した文字の場所を取得する・strchr>文字列/指定した文...
-[[先頭から一致する文字列数を求める・strspn>文字列/先頭か...
-[[指定した文字列が存在する位置を返却する・strstr>文字列/...
-[[大文字小文字を区別せず指定した文字列が存在する位置を返...
* strstr書式 [#g54ff1e2]
strstrの書式を以下に記します。
-必要なインクルードファイル
#include <string.h>
-strstrの書式
char *strstr(const char *haystack, const char *needle);
-戻り値~
発見した位置の文字列のポインタ
-引数~
1つ目: 対象文字列。~
2つ目: 検索文字列
* strstrのCサンプルコード [#mec26a81]
&ref(strstr.c); (改行コードLF)
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s = "abcdefghijklmnopqrstuvwxyz";
char *p;
p = strstr(s, "def");
if (p != NULL) {
printf("%s\n", p);
}
p = strstr(s, "fed");
if (p != NULL) {
printf("%s\n", p);
}
else {
printf("fed string is not found.\n");
}
return 0;
}
* strstrのCサンプルコードの実行結果 [#i20a71c9]
コンパイルし実行した結果を以下に記します。
$ gcc strstr.c -o strstr
$ ./strstr
defghijklmnopqrstuvwxyz
fed string is not found.
以上、strstr関数のCサンプルコードでした。
#htmlinsertpcsp(c-btm.html,c-sp.html)
終了行:
#navi(../)
* 指定した文字列が存在する位置を返却する・strstr [#v740a...
strstr関数は引数で指定した文字列が存在した場合、存在した...
以下にCサンプルコードと実行例を記します。
#contents
#htmlinsertpcsp(c-top.html,c-sp.html)
* 関連記事 [#u9fbbeba]
-[[指定した文字の場所を取得する・strchr>文字列/指定した文...
-[[先頭から一致する文字列数を求める・strspn>文字列/先頭か...
-[[指定した文字列が存在する位置を返却する・strstr>文字列/...
-[[大文字小文字を区別せず指定した文字列が存在する位置を返...
* strstr書式 [#g54ff1e2]
strstrの書式を以下に記します。
-必要なインクルードファイル
#include <string.h>
-strstrの書式
char *strstr(const char *haystack, const char *needle);
-戻り値~
発見した位置の文字列のポインタ
-引数~
1つ目: 対象文字列。~
2つ目: 検索文字列
* strstrのCサンプルコード [#mec26a81]
&ref(strstr.c); (改行コードLF)
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s = "abcdefghijklmnopqrstuvwxyz";
char *p;
p = strstr(s, "def");
if (p != NULL) {
printf("%s\n", p);
}
p = strstr(s, "fed");
if (p != NULL) {
printf("%s\n", p);
}
else {
printf("fed string is not found.\n");
}
return 0;
}
* strstrのCサンプルコードの実行結果 [#i20a71c9]
コンパイルし実行した結果を以下に記します。
$ gcc strstr.c -o strstr
$ ./strstr
defghijklmnopqrstuvwxyz
fed string is not found.
以上、strstr関数のCサンプルコードでした。
#htmlinsertpcsp(c-btm.html,c-sp.html)
ページ名: