Codechef showing wrong answer despite of correct output.

The code for the question :

 #include<stdio.h>
 #include<string.h>
 #include<ctype.h>
int main(){
   char name[35];
   char rname[35];
   int testNumber,count=0,i,j=0,checkCount=0;

   scanf("%d",&testNumber);
   fflush(stdin);
   for(;testNumber>0;testNumber--){
        gets(name);
        for(i=0;name[i]!='\0';i++){
            if(name[i]==' ')
                count++;
        }if(count!=0){
        for(i=0;i<strlen(name)+1;i++){
            if(i==0&&count!=0){
                rname[j]=toupper(name[i]);j++;
                rname[j]='.';j++;
                rname[j]=' ';j++;
            }
            if(i!=0&&name[i-1]==' '&&checkCount!=count){
                    checkCount++;
            if(checkCount!=count){
                rname[j]=toupper(name[i]);j++;
                rname[j]='.';j++;
                rname[j]=' ';j++;
            }

            }
            if(checkCount==count){
                    if(name[i-1]==' '){
                        rname[j]=toupper(name[i]);j++;
                    }
                else{rname[j]=tolower(name[i]);
                j++;}
            }


        }
}
      else{
            for(i=0;i<strlen(name)+1;i++){
                if(i==0){
                    rname[j]=toupper(name[i]);
                    j++;
                }
                else{
                    rname[j]=tolower(name[i]);
                    j++;
                }
            }
      }

        puts(rname);
        j=0;
        count=0;
        checkCount=0;

   }


}

Can you point out where am I wrong?

But your solution is giving wrong output in codechef IDE!

Input:
3
gandhi
mahatma gandhI
Mohndas KaramChand gandhi

Output:

Gandhi
M. Gandhi

use getchar() after scanf("%d",&testNumber);

scanf("%d",&testNumber);
fflush(stdin);
add getchar();
This getchar() will stop the Extra enter character to be read by gets(name)…This is the one bug in ur code…but still its WA… I will post the bug after i debug it completely

Just Replace The last puts with printf and AC :-)…puts was printing some extra new lines i think

https://www.codechef.com/viewsolution/15026253

puts doesn’t need to be replaced! Only problem is not adding getchar(). see this:

https://www.codechef.com/viewsolution/15026097

Can you please explain me why this problem is there in codechef compiler but in codeblocks it ran fine.And why fflush is not working here since it also clears the buffer so the enter key should have not be read by the codechef compiler.

1 Like