1

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>
#include<stdlib.h>
#include<string.h>

#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 && (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!

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Majid NK
  • 311
  • 1
  • 3
  • 16

2 Answers2

4

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"
Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • Thank you for your answer and the link – Majid NK Jun 09 '15 at 06:53
  • 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! – Majid NK Jun 09 '15 at 07:58
  • @MajidNK , Use `size_t ln = strlen(Pattern); if(ln != 0) Pattern[ln-1] = '\0';`. You can do the same for `Text` if you wish – Spikatrix Jun 09 '15 at 08:00
  • Where are you using it? Use that just after the second `fgets`. – Spikatrix Jun 09 '15 at 08:07
  • 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. – Majid NK Jun 09 '15 at 08:11
  • 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. – Spikatrix Jun 09 '15 at 08:15
  • 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 – Majid NK Jun 09 '15 at 08:34
  • 1
    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 – Majid NK Jun 09 '15 at 09:19
1

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    --> "BANGALORE IS GARDEN CITY\n"
Pattern --> "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).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261