{"id":25403,"date":"2017-10-15T18:12:39","date_gmt":"2017-10-15T12:42:39","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25403"},"modified":"2017-10-15T18:12:39","modified_gmt":"2017-10-15T12:42:39","slug":"searching-patterns-set-1-naive-pattern-searching","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/searching-patterns-set-1-naive-pattern-searching\/","title":{"rendered":"C Programming-Searching for Patterns Set 1 Naive Pattern Searching"},"content":{"rendered":"<p>Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n &gt; m.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input:  txt[] = \"THIS IS A TEST TEXT\"\r\n        pat[] = \"TEST\"\r\nOutput: Pattern found at index 10\r\n\r\nInput:  txt[] =  \"AABAACAADAABAABA\"\r\n        pat[] =  \"AABA\"\r\nOutput: Pattern found at index 0\r\n        Pattern found at index 9\r\n        Pattern found at index 12\r\n\r\n<img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25410\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/C-Programming-Searching-for-Patterns-Set-1-Naive-Pattern-Searching.png\" alt=\"C Programming-Searching for Patterns Set 1 Naive Pattern Searching\" width=\"704\" height=\"384\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/C-Programming-Searching-for-Patterns-Set-1-Naive-Pattern-Searching.png 704w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/C-Programming-Searching-for-Patterns-Set-1-Naive-Pattern-Searching-300x164.png 300w\" sizes=\"(max-width: 704px) 100vw, 704px\" \/><\/pre>\n<p>Pattern searching is an important problem in computer science. When we do search for a string in notepad\/word file or browser or database, pattern searching algorithms are used to show the search results.<\/p>\n<p>Naive Pattern Searching:<br \/>\nSlide the pattern over text one by one and check for a match. If a match is found, then slides by 1 again to check for subsequent matches.<\/p>\n<p><strong>C Programming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program for Naive Pattern Searching algorithm<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;string.h&gt;<br\/> <br\/>void search(char *pat, char *txt)<br\/>{<br\/>    int M = strlen(pat);<br\/>    int N = strlen(txt);<br\/>  <br\/>    \/* A loop to slide pat[] one by one *\/<br\/>    for (int i = 0; i &lt;= N - M; i++)<br\/>    {<br\/>        int j;<br\/>  <br\/>        \/* For current index i, check for pattern match *\/<br\/>        for (j = 0; j &lt; M; j++)<br\/>            if (txt[i+j] != pat[j])<br\/>                break;<br\/> <br\/>        if (j == M)  \/\/ if pat[0...M-1] = txt[i, i+1, ...i+M-1]<br\/>           printf(&quot;Pattern found at index %d \\n&quot;, i);<br\/>    }<br\/>}<br\/>  <br\/>\/* Driver program to test above function *\/<br\/>int main()<br\/>{<br\/>   char txt[] = &quot;AABAACAADAABAAABAA&quot;;<br\/>   char pat[] = &quot;AABA&quot;;<br\/>   search(pat, txt);<br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming-Searching for Patterns Set 1 Naive Pattern Searching &#8211; Searching and Sorting &#8211; Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[].<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82927,1,71670],"tags":[71314,73202,73207,73225,73172,73213,7378,73216,72827,73163,73226,73158,73164,73218,73168,73230,73224,73167,73219,73179,73205,73210,73156,73166,73177,73176,73222,73196,73221,73162,73228,73229,73203,73208,73227,73220,70097,70308,73184,73157,73187,73201,73173,73165,73223,73160,73199],"class_list":["post-25403","post","type-post","status-publish","format-standard","hentry","category-c-programming-2","category-coding","category-searching-and-sorting","tag-algorithm-in-c","tag-c-compare-strings","tag-c-copy-string","tag-c-find-string","tag-c-programming-string","tag-c-search-string","tag-c-string","tag-c-string-append","tag-c-string-array","tag-c-string-compare","tag-c-string-comparison","tag-c-string-functions","tag-c-string-h","tag-c-string-handling","tag-c-string-library","tag-c-string-list","tag-c-string-pointer","tag-c-string-search","tag-c-string-terminator","tag-c-string-type","tag-c-match","tag-c-pattern-matching","tag-c-programming-searching-for-patterns-set-1-naive-pattern-searching","tag-cstring-find","tag-cstring-replace","tag-hstring","tag-kmp-string-matching-algorithm","tag-kmpfaster","tag-knuth-morris-pratt","tag-match-c","tag-matching-algorithm","tag-pattern-matching","tag-regular-expression-matching","tag-regular-expression-pattern","tag-regular-expression-pattern-matching","tag-return-string-c","tag-searching-algorithms-in-c","tag-searching-c","tag-string-algorithms","tag-string-c","tag-string-c-programming","tag-string-compare-c","tag-string-h","tag-string-hc","tag-string-in-programming","tag-the-c-string","tag-73199"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25403"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25403\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}