😸
[C] strstr 関数
機能
文字列の部分一致検索[1]
使用例
hello, world!
から world
を検索
\n
は改行を表す エスケープシーケンス[2]
コード
strstr("hello, world!", "world");
全文
#include <stdio.h>
#include <string.h>
void main() {
const char *text = "hello, world!";
const char *find = "world";
char *ptr = strstr(text, find);
if (ptr != NULL) {
printf("%s\n", ptr);
} else {
printf("見つかりませんでした\n");
}
}
実行結果
world!
sample
から abc
を検索(見つからない例)
コード
strstr("sample", "abc");
全文
#include <stdio.h>
#include <string.h>
void main() {
const char *text = "sample";
const char *find = "abc";
char *ptr = strstr(text, find);
if (ptr != NULL) {
printf("%s\n", ptr);
} else {
printf("見つかりませんでした\n");
}
}
実行結果
見つかりませんでした
Discussion