WROMG ANSWER!!!- Holes in the text

#include<stdio.h>
int holes(char);
int main()
{
char text[100];
int T,ans[40],i=0,j=0,k=0;
scanf("%d",&T);
k=T;
while(T–)
{
fflush(stdin);
gets(text);
ans[i]=0;
j=0;
while(text[j]!=’\0’)
{
ans[i]=ans[i]+holes(text[j]);
j++;
}
i++;
}
for(i=0;i<k;i++)
printf("\n%d",ans[i]);
return 0;
}
int holes(char ch)
{
switch(ch)
{
case ‘B’: return 2;
case ‘A’:
case ‘D’:
case ‘O’:
case ‘P’:
case ‘Q’:
case ‘R’: return 1;
default : return 0;
}
}
what’s the problem in this code???

1 Like

Check input

2
B
B

:wink:

it’s working…but in my pc

what’s the output on your computer, put this input to holes.in file and run

holes.exe < holes.in > holes.out

and paste here content of your holes.out file

The problem is with the input.
Do not use gets after scanf.
I had answered the similar issue/problem before in code-chef discussion forum.
Refer these Link::

Link1

Link 2

Link 3

In order to resolve it:

one way is:

Instead of using scanf for integer input,use gets to take input as char array and then convert it into integer using atoi function of <stdlib.h>

[Note:There are several other ways to fix this problem.]

I have removed the bug ,fix it , will get accepted:)

..........
 char text[100];
 int T,ans[40],i=0,j=0,k=0;
 char tk[5];
 gets(tk);//Take number of test case as char array
 T=atoi(tk);//parse to integer using atoi function of <stdlib.h>
 k=T;
 while(T--)
 {
	.......
2 Likes
//