{"id":25404,"date":"2017-10-15T18:15:19","date_gmt":"2017-10-15T12:45:19","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25404"},"modified":"2017-10-15T18:15:19","modified_gmt":"2017-10-15T12:45:19","slug":"c-programming-searching-patterns-set-1-naive-pattern-searching","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-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-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp 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":[83515,1,71670],"tags":[71314,73193,73186,73172,73170,7378,73163,73178,73175,73158,73164,73168,73198,73167,73190,73179,73156,73166,73177,73176,73169,73182,73183,73194,73171,73196,73189,73195,73162,73191,73192,73161,73159,73174,70272,70671,70308,73184,73157,73187,73173,73165,73185,73188,73181,73180,73160,73197,73199],"class_list":["post-25404","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-searching-and-sorting","tag-algorithm-in-c","tag-boyer-moore","tag-boyer-moore-algorithm","tag-c-programming-string","tag-c-programs-on-strings","tag-c-string","tag-c-string-compare","tag-c-string-concatenation","tag-c-string-copy","tag-c-string-functions","tag-c-string-h","tag-c-string-library","tag-c-string-programming","tag-c-string-search","tag-c-string-set","tag-c-string-type","tag-c-programming-searching-for-patterns-set-1-naive-pattern-searching","tag-cstring-find","tag-cstring-replace","tag-hstring","tag-karp-rabin","tag-karp-rabin-algorithm","tag-kmp","tag-kmp-algorithm","tag-kmp-algorithm-example","tag-kmpfaster","tag-knuth-algorithm","tag-m1txt","tag-match-c","tag-naive-algorithm","tag-naive-string-matching-algorithm","tag-rabin-karp","tag-rabin-karp-algorithm","tag-rabin-karp-algorithm-example","tag-search-algorithms","tag-search-string","tag-searching-c","tag-string-algorithms","tag-string-c","tag-string-c-programming","tag-string-h","tag-string-hc","tag-string-library-c","tag-string-match","tag-string-matching-algorithms","tag-string-search-c","tag-the-c-string","tag-73197","tag-73199"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25404","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=25404"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25404\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}