Codechef showing wrong answer despite of correct output.

c
codeblocks
codechef
output
wrong-answer

#1

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*!='\0';i++){
            if(name*==' ')
                count++;
        }if(count!=0){
        for(i=0;i<strlen(name)+1;i++){
            if(i==0&&count!=0){
                rname[j]=toupper(name*);j++;
                rname[j]='.';j++;
                rname[j]=' ';j++;
            }
            if(i!=0&&name[i-1]==' '&&checkCount!=count){
                    checkCount++;
            if(checkCount!=count){
                rname[j]=toupper(name*);j++;
                rname[j]='.';j++;
                rname[j]=' ';j++;
            }

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


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

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

   }


}

Can you point out where am I wrong?


#2

But your solution is giving wrong output in codechef IDE!

Input:
3
gandhi
mahatma gandhI
Mohndas KaramChand gandhi

Output:

Gandhi
M. Gandhi

#3

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


#4

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


#5


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


#6

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


#7

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

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


#8

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.