0
Code Coach Compiler Glitch?
I am trying to run the following code for the "Convert US date to EU date" code coach problem for C. https://code.sololearn.com/cj38mh2IglNd/# It doesn't pass the second and third test cases. The second test case is: January 1, 2021. The strange this is that when I input this in the code playground compiler, I get the expected result, but when code-coach runs my program, it claims that the output is: 1 instead of: 1/1/2021 which it is when I run the code in the code playground. I think there is something wrong with the code-coach compiler. Any suggestions?
2 ответов
+ 5
There is nothing wrong with the compiler, you're just printing invisible null characters.
For example when I input "January 1, 2021" I get the output "1/1/2021", but in reality the output is "1\0/1\0/2021" where \0 is the null character and to the compiler, these 2 don't match.
The code coach probably reads the string until the first null character, that's why you only get '1' there.
The problem is that you print %c%c ( try changing it to %d %d to see the 0s ) for both day and month, but what if the day or month only has 1 character?
Then the 2nd index will be 0, or null and you still print it regardless.
Also, consider using strcmp or strncmp instead of manually comparing each index for the month. :)
+ 1
Thanks, I fixed it. Certainly not pretty as other solutions, but it passes all cases nonetheless. I will akm to make better c code in the future.