Can't use gets instead of scanf?

Here is my code for HOLES problem:-

#include<stdio.h>

int main()
{
	int t,count,i;
	char str[100];
	scanf("%d",&t);
	while(t>0)
	{
		i=0;count=0;
		scanf("%s",str);
		while(str[i]!='\0')
		{
			switch (str[i])
			{
				case 'A':
				case 'D':
				case 'O':
				case 'P':
				case 'Q':
				case 'R':
				count++;
				break;
				case 'B':
				count+=2;
				break;
			}
			i++;
		}
		printf("%d\n",count);
		t--;
	}
	return 0;
}

This works fine but if I put gets(str) instead of scanf("%s",str) then it gives me a wrong ans. Can someone explain why?

The input might be having blank line after completion of 1 test case.

gets() simply reads a line, so if you are using blank lines in your input, it would be considered as a string with length=0.

Consider the following test case:

2
CODECHEF
DRINKEATCODE

1 Like

Alright…Thanks!!

If thats the case, you can try putting a getchar() before gets() to remove the newline character from the buffer.

but you don’t know how many blank lines user will input, and therefore how many newline characters are…scanf() will take care even if there are 2 or more blank lines