> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/k/knuth-morris-pratt_string_search.md).

# Knuth-Morris-Pratt string search

```ruby
func kmp_search (String S, String W) {

    S.graphemes!
    W.graphemes!

    func kmp_table {
        var (pos, cnd, *T) = (1, 0, -1)
        for (nil; pos < W.len; (++pos; ++cnd)) {
            if (W[pos] == W[cnd]) {
                T[pos] = T[cnd]
            }
            else {
                T[pos] = cnd
                while ((cnd >= 0) && (W[pos] != W[cnd])) {
                    cnd = T[cnd]
                }
            }
        }
        T[pos] = cnd
        return T
    }

    var (k, T, *I) = (0, kmp_table())

    for (var j = 0; j < S.len; nil) {
        if (W[k] == S[j]) {
            ++j
            ++k
            if (k == W.len) {
                I << (j - k)
                k = T[k]
            }
        }
        elsif ((k = T[k]) < 0) {
            ++j
            ++k
        }
    }

    return I
}

var texts = [
    "GCTAGCTCTACGAGTCTA",
    "GGCTATAATGCGTA",
    "there would have been a time for such a word",
    "needle need noodle needle",
    "BharôtভাৰতBharôtভারতIndiaBhāratભારતBhāratभारतBhārataಭಾರತBhāratभारतBhārat"+
    "amഭാരതംBhāratभारतBhāratभारतBharôtôଭାରତBhāratਭਾਰਤBhāratamभारतम्Bārataபாரத"+
    "ம்BhāratamഭാരതംBhāratadēsamభారతదేశం",
    "InhisbookseriesTheArtofComputerProgrammingpublishedbyAddisonWesleyDKnu"+
    "thusesanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblyl"+
    "anguagestoillustratetheconceptsandalgorithmsastheyarepresented",
    "Nearby farms grew a half acre of alfalfa on the dairy's behalf, with ba"+
    "les of all that alfalfa exchanged for milk.",
]

var pats = ["TCTA", "TAATAAA", "word", "needle", "ഭാരതം", "put", "and", "alfalfa"]

texts.each_kv {|k,v| say "text#{k} = #{v}" }; say ''

pats.each_kv {|k,v|
    var j = (k < 6 ? k : k-1)
    say ("Found '#{v}' in 'text#{j}' at indices ", kmp_search(texts[j], v))
}
```

#### Output:

```
text0 = GCTAGCTCTACGAGTCTA
text1 = GGCTATAATGCGTA
text2 = there would have been a time for such a word
text3 = needle need noodle needle
text4 = BharôtভাৰতBharôtভারতIndiaBhāratભારતBhāratभारतBhārataಭಾರತBhāratभारतBhāratamഭാരതംBhāratभारतBhāratभारतBharôtôଭାରତBhāratਭਾਰਤBhāratamभारतम्Bārataபாரதம்BhāratamഭാരതംBhāratadēsamభారతదేశం
text5 = InhisbookseriesTheArtofComputerProgrammingpublishedbyAddisonWesleyDKnuthusesanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguagestoillustratetheconceptsandalgorithmsastheyarepresented
text6 = Nearby farms grew a half acre of alfalfa on the dairy's behalf, with bales of all that alfalfa exchanged for milk.

Found 'TCTA' in 'text0' at indices [6, 14]
Found 'TAATAAA' in 'text1' at indices []
Found 'word' in 'text2' at indices [40]
Found 'needle' in 'text3' at indices [0, 19]
Found 'ഭാരതം' in 'text4' at indices [68, 138]
Found 'put' in 'text5' at indices [26, 90]
Found 'and' in 'text5' at indices [101, 128, 171]
Found 'alfalfa' in 'text6' at indices [33, 87]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/k/knuth-morris-pratt_string_search.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
