C言語において、文字列の検索は様々な方法で行えます。
ここでは、strstr関数を使用せずに文字列の検索を行う方法と、strstr関数を用いた検索方法について解説します。
これにより、異なる手法で文字列を効果的に探索できるようになります。
strstrを使わないで文字列検索する
strstr関数を使用せずに文字列の検索を行うには、
文字列を一文字ずつ比較しながら検索する方法があります。
#include <stdio.h>
// 文字列を検索する関数
int searchSubstring(const char* haystack, const char* needle) {
    // メインの検索ループ
    for (int i = 0; haystack[i] != '#include <stdio.h>
// 文字列を検索する関数
int searchSubstring(const char* haystack, const char* needle) {
    // メインの検索ループ
    for (int i = 0; haystack[i] != '\0'; i++) {
        // ニードルの先頭文字と一致する位置を見つける
        if (haystack[i] == needle[0]) {
            // ニードル全体が一致しているか確認
            int j;
            for (j = 0; needle[j] != '\0'; j++) {
                if (haystack[i + j] != needle[j]) {
                    break;
                }
            }
            // ニードル全体が一致した場合
            if (needle[j] == '\0') {
                return i; // 一致位置を返す
            }
        }
    }
    return -1; // 見つからなかった場合
}
int main() {
    const char* text = "This is a sample text.";
    const char* pattern = "sample";
    // 文字列を検索
    int position = searchSubstring(text, pattern);
    // 結果を表示
    if (position != -1) {
        printf("Pattern found at position: %d\n", position);
    } else {
        printf("Pattern not found.\n");
    }
    return 0;
}
'; i++) {
        // ニードルの先頭文字と一致する位置を見つける
        if (haystack[i] == needle[0]) {
            // ニードル全体が一致しているか確認
            int j;
            for (j = 0; needle[j] != '#include <stdio.h>
// 文字列を検索する関数
int searchSubstring(const char* haystack, const char* needle) {
    // メインの検索ループ
    for (int i = 0; haystack[i] != '\0'; i++) {
        // ニードルの先頭文字と一致する位置を見つける
        if (haystack[i] == needle[0]) {
            // ニードル全体が一致しているか確認
            int j;
            for (j = 0; needle[j] != '\0'; j++) {
                if (haystack[i + j] != needle[j]) {
                    break;
                }
            }
            // ニードル全体が一致した場合
            if (needle[j] == '\0') {
                return i; // 一致位置を返す
            }
        }
    }
    return -1; // 見つからなかった場合
}
int main() {
    const char* text = "This is a sample text.";
    const char* pattern = "sample";
    // 文字列を検索
    int position = searchSubstring(text, pattern);
    // 結果を表示
    if (position != -1) {
        printf("Pattern found at position: %d\n", position);
    } else {
        printf("Pattern not found.\n");
    }
    return 0;
}
'; j++) {
                if (haystack[i + j] != needle[j]) {
                    break;
                }
            }
            // ニードル全体が一致した場合
            if (needle[j] == '#include <stdio.h>
// 文字列を検索する関数
int searchSubstring(const char* haystack, const char* needle) {
    // メインの検索ループ
    for (int i = 0; haystack[i] != '\0'; i++) {
        // ニードルの先頭文字と一致する位置を見つける
        if (haystack[i] == needle[0]) {
            // ニードル全体が一致しているか確認
            int j;
            for (j = 0; needle[j] != '\0'; j++) {
                if (haystack[i + j] != needle[j]) {
                    break;
                }
            }
            // ニードル全体が一致した場合
            if (needle[j] == '\0') {
                return i; // 一致位置を返す
            }
        }
    }
    return -1; // 見つからなかった場合
}
int main() {
    const char* text = "This is a sample text.";
    const char* pattern = "sample";
    // 文字列を検索
    int position = searchSubstring(text, pattern);
    // 結果を表示
    if (position != -1) {
        printf("Pattern found at position: %d\n", position);
    } else {
        printf("Pattern not found.\n");
    }
    return 0;
}
') {
                return i; // 一致位置を返す
            }
        }
    }
    return -1; // 見つからなかった場合
}
int main() {
    const char* text = "This is a sample text.";
    const char* pattern = "sample";
    // 文字列を検索
    int position = searchSubstring(text, pattern);
    // 結果を表示
    if (position != -1) {
        printf("Pattern found at position: %d\n", position);
    } else {
        printf("Pattern not found.\n");
    }
    return 0;
}
この方法では、ヘイスティングと呼ばれる検索位置を前進させながら、
一致するかどうかを一文字ずつ比較しています。
strstr関数で文字列のチェック
一方で、C言語標準ライブラリには文字列を検索するための strstr 関数が用意されています。
以下はその基本的な使用例です。
#include <stdio.h> 
#include <string.h>
int main() {
    const char* text = "This is a sample text.";
    const char* pattern = "sample";
    // 文字列を検索
    const char* result = strstr(text, pattern);
    // 結果を表示
    if (result != NULL) {
        printf("Pattern found at position: %ld\n", result - text);
    } else {
        printf("Pattern not found.\n");
    }
    return 0;
}
strstr関数を使用することで、簡潔かつ効率的に文字列を検索できます。
関数の返り値は見つかった位置の先頭を指すポインタであり、見つからなかった場合は NULL が返ります。
注意点
- 手動で文字列を検索する場合、ヘイスティングと呼ばれる検索位置を管理する必要があります。
 - strstr関数を使用する際は、見つからなかった場合に注意してエラーハンドリングを行いましょう。
 
広告
まとめ
C言語において文字列の検索は、手動で一文字ずつ比較する方法と、
strstr関数を使用する方法があります。
どちらを選ぶかは、状況によりますので、適切な方法を選択しましょう。
