title: Why the behaviour differs when fgets() is used instead gets()
body: <p>as per <code>gcc</code> warning: </p>
  
  <blockquote>
    <p>the `gets' function is dangerous and should not be used</p>
  </blockquote>
  
  <p>I try to use <code>fgets()</code> but it does not wok out in my code as you may see the outputs for both at the end of code below. May someone please tell me what mistake (if any) I am doing?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  
  #define size 128
  
  int Table[size];
  
  /****ShiftTable Function Declaration****/
  void ShiftTable(char Pattern[]);
  
  /****Horspool Function Declaration****/
  int Horspool(char Pattern[],char Text[]);
  
  /****Main Function****/
  int main()
     {
      char Text[100],Pattern[100];
      int pos;
  
      printf("Enter the Text : ");
      gets(Text);                    //Works fine
      //fgets(Text,100,stdin);       //Does not work
  
      printf("\nEnter the Pattern: ");
      gets(Pattern);                 //Works fine
      //fgets(Pattern,100,stdin);    //Does not Work
  
      pos=Horspool(Pattern,Text);
  
      if(pos==-1)
              printf("\nPattern Not Found!\n\n");
      else
          printf("\nPattern Found at %d position.\n\n",pos+1);        
  
      return 0;
     }
  
  /****Horspool Function Definition****/
  int Horspool(char Pattern[],char Text[])
     {
      int m,n,i,k;
  
      n=strlen(Text);
      m=strlen(Pattern);
  
      ShiftTable(Pattern);
  
      i=m-1;
  
      while(i&lt;n)
           {
          k=0;
          while(k&lt;m &amp;&amp; (Text[i-k]==Pattern[m-1-k]))  k++;
  
          if(k==m)   return i-m+1;
  
          i+=Table[Text[i]];
           }
  
      return -1;
      }
  
  /****ShifTable Function Definition****/
  void ShiftTable(char Pattern[])
      {
      int i,m;
      m=strlen(Pattern);
  
      for(i=0;i&lt;size;i++)
          Table[i]=m;
  
      for(i=0;i&lt;m-1;i++)
          Table[Pattern[i]]=m-1-i;
      }
  </code></pre>
  
  <p><strong><em>Output:</em></strong></p>
  
  <p>When I use <code>gets()</code></p>
  
  <blockquote>
    <p>majid@majid-K53SC:~ $ ./a.out</p>
    
    <p>Enter the Text : BANGALORE IS GARDEN CITY</p>
    
    <p>Enter the Pattern: GARDEN</p>
    
    <p>Pattern Found at 14 position.</p>
  </blockquote>
  
  <p>When I use <code>fgets()</code></p>
  
  <blockquote>
    <p>majid@majid-K53SC:~ $ ./a.out</p>
    
    <p>Enter the Text : BANGALORE IS GARDEN CITY</p>
    
    <p>Enter the Pattern: GARDEN</p>
    
    <p>Pattern Not Found!</p>
  </blockquote>