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ā¦