Title: Why the behaviour differs when fgets() is used instead gets()
Tags: c;fgets;gets
Question: as per gcc warning:
the `gets' function is dangerous and should not be used
I try to use fgets() 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?
#include<stdio.h&gt;
#include<stdlib.h&gt;
#include<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<n)
{
k=0;
while(k<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<size;i++)
Table[i]=m;
for(i=0;i<m-1;i++)
Table[Pattern[i]]=m-1-i;
Output:
When I use gets()
majid@majid-K53SC:~ $ ./a.out
Enter the Text : BANGALORE IS GARDEN CITY
Enter the Pattern: GARDEN
Pattern Found at 14 position.
When I use fgets()
majid@majid-K53SC:~ $ ./a.out
Enter the Text : BANGALORE IS GARDEN CITY
Enter the Pattern: GARDEN
Pattern Not Found!

Here is the accepted answer: fgets consumes the newline character (you press Enter after entering the string) and stores it in Text while gets does not. You'll need to strip the newline character off from Text.
So, after both the calls to gets,
Text = "BANGALORE IS GARDEN CITY"
Pattern = "GARDEN"
and in the case of fgets,
Text = "BANGALORE IS GARDEN CITY\n"
Pattern = "GARDEN\n"
Comment for this answer: Thank you for your answer and the link
Comment for this answer: I tried two method given in the link but they didn't work out, I don.t know what is the problem... I'm getting tired of fgets() ! please tell me what is wrong:
either   strtok(Pattern,"\n");
or        ln = strlen(Pattern);
Pattern[ln] = '\0';
makes it to go to infinite loop!
Comment for this answer: yes I'm using exactly after second fgets() and interestingly when pattern is something that matches  from 1st position of Text it works but any other thing it goes to infinite loop. It should be something with fgets as gets() works fine.
Comment for this answer: I don't think code is wrong as it works with gets() but sure I try some more times with code and if no hope I ll ask new question. Thanks for your time
Comment for this answer: Got it :) It was problem of size //size must be atleast 128 to include asci codes of all keys in keyboard... it was 100 so for upper-case letter had no problem but when lower case was input it had gone to infinite loop
Comment for this answer: @MajidNK , Use `size_t ln = strlen(Pattern); if(ln != 0) Pattern[ln-1] = '\0';`. You can do the same for `Text` if you wish
Comment for this answer: Where are you using it? Use that just after the second `fgets`.
Comment for this answer: Hmm. Maybe something is wrong with your code. Could you post the updated code? You can also post a new question giving relevant details if you want.

Here is another answer: First, thank you for paying heed to the warning. You're very right to switch to fegts().
Now, to answer your question,
Why the behaviour differs when  fgets() is used instead gets()?
That is not a weird behaviour. That is the expected behaviour of fgets().
Unlike gets(), fgets() reads and stores the trailing newline into the supplied input buffer. You need to strip off the newline before you go on for the comparison.
Without that, essentially, your inputs look like:
Text    --&gt; "BANGALORE IS GARDEN CITY\n"
Pattern --&gt; "GARDEN\n"
Now, as we know, a "GARDEN " and "GARDEN\n" are not same, so your comparison fails.
That said, just a suggestion, the recommended signature of main() is int main(void).

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/09/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><fgets><gets>', 'Title': 'Why the behaviour differs when fgets() is used instead gets()', 'ViewCount': 109, 'id': 30724675, 'user_AboutMe': '', 'user_AccountId': 5692837.0, 'user_DisplayName': 'Majid NK', 'user_Reputation': 311.0, 'user_Views': 134.0, 'user_id': 4500329.0}}