title: C- pattern matching
body: <p>I am trying to write a program that receives input infinitely and whenever an input sequence matches a given pattern it should print that a match has been found and continue searching for other occurrences of the pattern I managed just to code this</p>
  
  <pre><code>   #include&lt;stdio.h&gt;
      #include&lt;string.h&gt;
      int main(){
        char ch;
        char pattern[4]="1234";
        int i=0;
        while(1){
          scanf(" %c",&amp;ch);
          if(ch==pattern[i]){
            i+=1;
          } else {
              i = 0;
          }
          if (i == 4) {
              printf("match found!\n");
              i = 0;
          }
          //printf("%c",ch);
        }
        return 0;
      }
  </code></pre>
  
  <p>The problem is that this code doesn't handle repetion cases like 11234 .</p>
  
  <p>my other approach uses buffering, that has some error</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  int main(){
    char ch;
    char pattern[4]="1234";
    char buf[4] = "";
    int i=0;
    while(1){
      scanf(" %c",&amp;ch);
      buf[i%4]=ch;
      i++;
      if(strcmp(pattern,buf)==0){
        printf("Match found");
      }
    }
    return 0;
  }
  </code></pre>
  
  <p>Help me fix the problem     </p>