Doubt in problem SNAKPROC snake procession

Question: CodeChef: Practical coding for everyone .This code works perfectly on my IDLE but shows wrong answer on codechef.Please let me know the changes needed:
#include<stdio.h>

int main()
{
int r;
scanf(“%d”,&r);
while(r–){
char a[500],b[500];
int n,i,k=0,f=0,c=0;
scanf(“%d”,&n);
scanf(“%s”,&a);
for(i=0;i<n;i++)
{
if(a[i]==‘H’)
{
b[k++]=a[i];
}
if(a[i]==‘T’)
{
b[k++]=a[i];
}

}
for(k=0;k<n;k++)
{
    if(b[k]=='\0')
    break;
    //printf("%c",b[k]);
    if(b[0]=='T')
    c++;
    else if(b[k]=='H' && b[k+1]=='H')
        c++;
    else if(b[k]=='H')
    f++;
    else if(b[k]=='T')
    f--;
    else if(b[n-1]=='H')
    c++;

}
//printf("%d %d",f,c);
if(c>0)
printf("invalid\n");
else if(f>0)
printf("invalid\n");
else
printf("valid\n");
b[0]='\0';

}
return 0;
}

Failing testcase-

1

10

H…T…T…H

there is a correction needed at line-{else if(b[n-1]==‘H’)}(that index n-1 should be changed). Also in the same for loop u should check if the value of variable “f” is either 0 or 1. If not the snake is invalid. any other value of “f” will give a invalid snake. (think why…)