WHY FOR LOOP IS NOT STOPPING?

int t;
scanf("%d",&t);
int i,l;
for(i=0;i<t;i++)
{   char str[100000];
    int counta=0,countb=0,countc=0;
    scanf("%s",str);
    l=strlen(str);
    printf("%d\n",l);
    int j=0,k=0,m=0;
    while(j<l)
    {
        if(str[j]=='A')
        {counta++;
        j++;
        }
        else if(str[j]=='B')
        {
            countb++;
            j++;
            
        }
        else if(str[j]=='.')
        {    countc=1;
            if(str[j-1]=='A')
            {
                j++;
                do
            {
                
                if(str[j]=='.')
                countc++;
                j++;
                }
                while(str[j]=='.');
                
                if(str[j]=='A')
                counta=counta+countc;
                j=j-1;
                j++;
                
        }
        
        else if(str[j-1]=='B')
            {  
                j++;
                do
            {
                
                if(str[j]=='.')
                countc++;
                j++;
                }
                while(str[j]=='.');
                
                if(str[j]=='B')
                countb=countb+countc;
                j=j-1;
                j++;
                
        }
        
    }
  
}

printf("%d %d\n",counta,countb);
}