Tourist Translations - WA

My code is working fine for all the sample testcases…then all its giving wrong answer!
link to my code is:
tourist translations
Thank you

Your code print only 4 lines to output - see

I’m always wondering why someone want to use gets() or some getchar() logic. My opinion is while there are no white spaces, using scanf( "%s", ... ) is a lot safer…

1 Like

You should add a dummy gets() just before the ‘for loop’ in order to read ‘newline character \n’. That’s the reason why your code outputs only N-1 lines.

Alternatively, you can do something like this:

#include <stdio.h>

int main()
    int i,j,t=0;
    char sentence[100];
    scanf("%d ", &i);
    while((j=getchar()) != '\n')
        sentence[t] = j;
    sentence[t+1] = '\0';

    int x, num = 0;
    for(x=0; sentence[x] != '\0';x++)
    printf("String has %d characters ", num);
    return 0;

This also skips the whitespace and correctly reads the string :slight_smile:

There are several ways :smiley:

Best regards,


1 Like

Actually this was working fine on my compiler…so i couldn’t figure out my mistake…
is it becoz of fflush(stdin)?

Anyways thanks

@jaguar >> Welcome to Codechef! For the problems like these, I would suggest please attend each and every contest. So that, after some time you will find no problem in Input/Output, then spend few more months, then you will find algorithms going easier as well. Its all about practice and learning as many things as you want. My trick is, before going to bed, every night, ask this simple question to yourself, what new thing(s) you learned today? That will keep you motivated. Good Luck!

1 Like

My code works fine on my PC but i got WA. please why? here is the link TOTR - java