title: C Programming; character pattern recognizing via finite state machine program
body: <p>I'm building a pattern recognizer program in C that reads in both a user-defined string of characters and a user-defined pattern of 4 characters. The program then has one function to determine if a pattern is found and where the pattern (in terms of the input text index) was initially found.</p>
  
  <p>I know this is elementary to most of you, and I just hope to quickly become a more proficient programmer. When I execute my program it gets stuck in an infinite loop in a manner I don't understand.</p>
  
  <p>I know the issue lies with my FindMatch function and not with the reading in of input text and pattern. What is wrong with my FindMatch function?! Please help.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  char *getCharBlock(int *size);
  int findmatchA(char *text, char *pattern, int tsize, int psize);
  void printIt(char *ptr, int index, int size);
  
  int main(){
     char *text, *pattern; //pointers for the characters you will read
     char *p,*q,*r; //some pointer variables
     int tsize,psize,x,y,z; //some integers
  
     printf("Please input a sequence of character text (characters only will be stored):");
     text = getCharBlock(&amp;tsize);
  
     printf(" Now input the pattern you seek to search for: ");
     pattern = getCharBlock(&amp;psize);
  
     x = findmatch(text,pattern,tsize, psize);
     if(x== -1){
        printf("No Match Found \n");
        printf("No starting position for match exists \n");
     }
     else{
        printf("Match Has Been Found! \n");
        printf("Match starting position at index %d \n", x);
        printf("Remaining text after Match: \n");
        printIt(text, x+psize, tsize);
     }
  
  
     free(text);
     free(pattern);
  }
  
  char *getCharBlock(int *size){
     char *input = (char*) malloc (80*sizeof(char));
     char a;
     int i = 0;
     a = getchar();
     while(i&lt;80 &amp;&amp; a!= '\n'){
  
        if( (a&gt;= 'a' &amp;&amp; a &lt;= 'z') || (a&gt;= 'A' &amp;&amp; a &lt;= 'Z') ){
           *(input + i) = a;
           i++;
        }
        a = getchar();
     }
     *size = i;
  
     return input;
   }
  
   int findmatch(char *text, char *pattern, int tsize, int psize) {
     int index = 0;
     int state = 0;
     while (psize &lt;= tsize) {
  
       if ((*(text + index) == *pattern) &amp;&amp; state == 0){
           state = 1;
           index++;
            printf( "test 1 \n");
       }
       else if ((*(text + index) != *pattern) &amp;&amp; state == 0){
            state = 0;
            index++;
            printf( "test1.1 \n");
       }
       else if (*(text + index) == *(pattern + 1) &amp;&amp; state ==1) {
         state = 2;
         index++;
         printf( "test 2 \n");
       }
       else if (*(text + index) != *(pattern + 1) &amp;&amp; state ==1) {
          state = 0;
          printf("test 2.2 \n");
       }
       else if (*(text + index) == *(pattern + 2) &amp;&amp; state ==2) {
          state  = 3;
          printf("test 3 \n");
       }
       else if (*(text + index) != *(pattern + 2) &amp;&amp; state ==2) {
          state  = 0;
          printf("test 3.3 \n");
       }
       else if (*(text + index) == *(pattern + 3) &amp;&amp; state ==3) {
          state = 4;
          printf("test 4 \n");
       }
       else if (*(text + index) != *(pattern + 3) &amp;&amp; state ==3) {
          state = 0;
          printf("test 4.4 \n");
       }
       else {
           return -1;
       }
       index++;
     }
     return index;
     }
  </code></pre>