same code without gets() give right answer! why not with gets(). Please explain someone.

#include <stdio.h>
#include<string.h>
int main(void)
{

	int i,c[28],d[28],e[28],l,l1,l2,t,p1,p2;
	char s[28],a[105],b[105];
	scanf("%d",&t);
	getchar();
	gets(s); 
	l=strlen(s);
	while(t--)
	{
		gets(a);
		gets(b);
		l1=strlen(a);
		l2=strlen(b);
		for(i=0;i<28;i++)
		{c[i]=0;
		d[i]=0;
			e[i]=0;}
		for(i=0;i<l;i++)
		{
			c[s[i]-97]=i;
			
		}
		for(i=0;i<l1;i++)
		d[a[i]-97]++;
		for(i=0;i<l2;i++)
		e[b[i]-97]++;
		p1=p2=0;
		for(i=0;i<26;i++)
		{
			if(d[i]!=0 && e[i]!=0)
			{
				if(d[i]>e[i])
				{
					p1=p1+c[i];
				}
				else if(d[i]<e[i])
				{
					p2=p2+c[i];
				}
				c[i]=0;
			}
		}
		
		for(i=0;i<l1;i++)
		{
			p1=p1+c[a[i]-97];
			c[a[i]-97]=0;
		}
		for(i=0;i<l2;i++)
		{
			p2=p2+c[b[i]-97];
			c[b[i]-97]=0;
		}
		
		if(p1==p2)
		puts("TIE");
		else if(p1>p2)
		puts("ALICE");
		else
		puts("BOB");
	}
	return 0;
}

The code seems to me of Stone Game.
Brother, never use gets() as it offers no protections against a buffer overflow vulnerability (that is, you cannot tell it how big the buffer you pass to it is, so it cannot prevent a user from entering a line larger than the buffer and clobbering memory). gets() is dangerous because it lets you read in more data than you’ve allocated space for, moreover it can take in more data than the size of the variable, thereby exposing the system to attacks and compromising security.

I hope I was able to clear your doubt.

1 Like