超快的搜索算法

1.textsearch.h

/* Super fast linear text search algorithms: searchi = search ignore case search = search case sensitive searchiw = search ignore case words only (e.g. words delimited by whitespace only, not words within words) searchw() = search case sensitive words only All functions return the number of matches for keyword in buffer, or -1 on error. by James Buchanan No license ristrictions on this code. Email: [email protected] */ #ifndef __TEXTSEARCH_H #define __TEXTSEARCH_H #include #include int searchi(const char *buffer, const char *keyword); int search(const char *buffer, const char *keyword); int searchiw(const char *buffer, const char *keyword); int searchw(const char *buffer, const char *keyword); #endif

2.textsearch.c

#include #include #include #include #include "textsearch.h" int searchi(const char *buffer, const char *keyword) { int k_len, b_len, ch_matches, found, i, j; b_len = strlen(buffer); k_len = strlen(keyword); if (!b_len || !k_len) return -1; ch_matches = found = 0; for (i=0; i

3.main

#include #include #include #include "textsearch.h" int main(void) { char buff[1024], key[56]; printf("Enter the buffer's contents ( to quit) ==>"); gets(buff); printf("Enter the keyword ( to quit) ==>"); gets(key); printf("/n/nTesting all 4 functions in textsearch.../n"); printf("Result for search() is %d./n", search(buff,key)); printf("Result for searchi() is %d./n", searchi(buff,key)); printf("Result for searchw() is %d./n", searchw(buff,key)); printf("Result for searchiw() is %d./n", searchiw(buff,key)); return 0; }

你可能感兴趣的:(超快的搜索算法)