Pig latin fails all test cases, but I don't see any difference.? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pig latin fails all test cases, but I don't see any difference.?

Hi guys, thanks for reading this. I was working on 'pig latin', and after failing a lot, I finally could solve it...well...maybe. After running the test cases, I don't know about the hidden ones, but the first 2 test cases of mine appeared to be alright, since the expected output and my output appeared to be the same. Yet I got all crosses. What's wrong here? Thanks! https://code.sololearn.com/cA22a201a1A2/?ref=app

14th May 2021, 3:51 AM
Md Shahriar Rahman
Md Shahriar Rahman - avatar
6 Answers
+ 2
The problem is the for loop. Since strings are zero-indexed, the last chracter is at position "length - 1", while at position "length" is the null character. However, you iterate over all characters including the null character, and as a consequence, print one in each word, which messes up the string comparison between your output and the solution. Just change it to for ( int i = 1; i < length; ++i ) { ... } and the code should pass all test cases.
14th May 2021, 7:06 AM
Shadow
Shadow - avatar
14th May 2021, 3:56 AM
Md Shahriar Rahman
Md Shahriar Rahman - avatar
0
This should be your output: "evermindnay ouveyay otgay hemtay" But i think it will look like this: "evermindnay ouveyay otgay hemtay " Could it be you have a space at the end?
14th May 2021, 4:43 AM
Paul
Paul - avatar
0
Paul This is exactly what I was thinking. So after the loop, I added a backspace [I hope it's correct: printf("\b\b");]. But the result didn't change, all failed
14th May 2021, 4:46 AM
Md Shahriar Rahman
Md Shahriar Rahman - avatar
0
Here's my solution: #include <stdio.h> int main() { char a[50]; fgets(a, 50, stdin); char k = *a; for (int i=1;a[i];i++) { if (a[i] == 32) printf("%cay ", k); else if (a[i-1] == 32) k = a[i]; else putchar(a[i]); } printf("%cay", k); return 0; } // Hope this helps
14th May 2021, 6:16 AM
Calvin Thomas
Calvin Thomas - avatar
0
Here's the strtok() solution: #include <stdio.h> #include <string.h> int main() { char a[50]; fgets(a, 50, stdin); char *t = strtok(a, " "); while (t) { printf("%s%cay ", t + 1, *t); t = strtok(0, " "); } return 0; } // Hope this helps
14th May 2021, 6:22 AM
Calvin Thomas
Calvin Thomas - avatar