holes in the text wrong answer

#include <stdio.h>
#include <string.h>

int main()
{
	int hole=0;
	int i;
	char word[100];
	
	scanf("%s",&word);
	fflush(stdin);	

	i=strlen(word);

	for(i=0;i<=40;i++)
	{
		if(word[i]=='A' || word[i]=='D' || word[i]=='O' || word[i]=='P' || word[i]=='R' || word[i]=='Q' )
		{
			hole++;
		}

		if(word[i]=='B')
		{
			hole+=2;
		}	

		if(word[i]>=0 && word[i]<=9)
		{
			break;
		}
				
	}
		printf("%d",hole);	
	
	getchar();
	return 0;

}

Well, there are a few problems, iā€™ll just list them quickly

Firstly you didnā€™t take care of input at start,number of test cases
, you need to take a input (say T) and run the code inside a loop iterating T times

Secondly in the for loop, you need to check from i=0 to i=strlen(word)-1 and not ā€˜40ā€™

You dont need this if condition

if(word[i]>=0 && word[i]<=9)
{
    break;
}

Take out the getchar() at the end

Then it will be fine i hope, try the correction yourself and see if you get it right

edit : add a new line after printing ā€œholesā€

Cheers

P.S - you can check my last submission of this problem, i did the mentioned changes and submitted it

also end ur line after every output printf("%d",hole); change with printf("%d\n",hole);

1 Like

I did not understand by what means you gave the loop till i<=40.
Shouldnā€™t it be i<strlen(word)?
Then in the question it is clearly given that the characters in the text will be from ā€˜Aā€™-ā€˜Zā€™
You need not check that caseā€¦Even if you check it it will not cause any wrong answer.But if we are sure that such a thing does not exist then why do we need to waste the time for checking that?
Moreover we need not pause the output instead using getchar().
Hope you got what i meantā€¦
Happy codingā€¦

  1. you Should make a loop like ā€œfor(j=0;j< i;j++)ā€ where ā€œi = strlen(word)ā€ instead of ā€œfor(i=0;i<=40;i++)ā€
  2. you donā€™t need to check the given inputā€¦ in the text file only capital letters are allowed(A-Z) so you donā€™t need to add extra if conditionā€¦ ā€œif(word[i]>=0 && word[i]<=9)ā€.
  3. and you donā€™t need to add getchar() at the endā€¦

keep show wrong answer why?? help pls

yeah made that change but forgot to mention

#include<stdio.h>
#include<string.h>
int main()
{
int hole=0;
int i;
char word[100];

scanf("%s",&word);
fflush(stdin);

i=strlen(word);

for(i=0;i<=strlen(word)-1;i++)
{
	if(word[i]=='A' || word[i]=='D' || word[i]=='O' || word[i]=='P' || word[i]=='R' || word[i]=='Q' )
	{
		hole++;
	}

	if(word[i]=='B')
	{
		hole+=2;
	}	
			
			
}
	printf("%d\n",hole);	


return 0;

}

i just input the answer like this. but keep wrong answer. i donā€™t know why. if i input 2 the output is 0. if i input other words it can compiled succesfully.

You still did not take the number of test cases, you need to take an int input first (say T) and then input word T time,
You donā€™t need to put the getchar() at the end.
Lastly while inputing the string in word[100], the scanf statement you used is wrong, ā€œ&wordā€ is not correct, it should be either only ā€œwordā€ or ā€œ&word[0]ā€

have a loot at this CodeChef: Practical coding for everyone

still if you need any more help, ask again