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;

}

keep show wrong answer why?? help pls

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);

yeah made that change but forgot to mention

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…

#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 http://www.codechef.com/viewsolution/3014972

still if you need any more help, ask again