I have worked this logic in the Codeblocks compiler and it works near perfect.
Why does it show a segmentation fault in codechef?
#include<stdio.h>
#include<string.h>
int main()
{
char a[20];
int count,i,n;
count=0;
scanf("%d",&n);
while(n–)
{
count=0;
printf("\nEnter the String:\t");
scanf("%s",a);
for(i=0;a[i]!=’\0’;i++)
{
switch(a[i])
{
case’A’:
case’O’:
case’P’:
case ‘R’:
case ‘D’:
count++;
break;
case ‘B’:
count+=2;
First thing you are not required to print anything except what you are asked to, So remove print statement printf(“Enter the String”); as this is not asked to print in problem.
Second thing in problem it is mentioned this line:
The length of the text is less then 100
So if you declare size of a as 20, there will be a sure case of overflow if size of input will be >20 and thus you are getting a segmentation fault.
Thus initialize a[105] and remove print statement…
@agneymenon
You need not to write anything except what you are asked to…and one more thing that i found is that you forgot to include ‘Q’ test case…‘Q’ contains one hole in it…