+ 1

Why this code is not working in Code Coach question "Pig Latin"??

You have two friends who are speaking Pig Latin to each other! Pig Latin is the same words in the same order except that you take the first letter of each word and put it on the end, then you add 'ay' to the end of that. ("road" = "oadray") Task Your task is to take a sentence in English and turn it into the same sentence in Pig Latin! Input Format A string of the sentence in English that you need to translate into Pig Latin. (no punctuation or capitalization) Output Format A string of the same sentence in Pig Latin. Sample Input "nevermind youve got them" Sample Output "evermindnay ouveyay otgay hemtay. *************************************************** Even though my answer is matching with required output. Code: https://code.sololearn.com/caGO1H2b3p0U/?ref=app

21st Jan 2021, 2:17 PM
Himanshu Sajwan
Himanshu Sajwan - avatar
2 odpowiedzi
+ 3
You need to change your condition on line 17 to if ( ... || i >= l ) { ... } because the case where i = l is already an access out of the bounds of the original string and appends another (likely invisible) character to "res", thus resulting in a failed string comparison between your output and the solution. You could also rewrite your loop alltogether into the somewhat shorter while ( s[ i ] != ' ' && i < l ) { res += s[ i++ ]; }
21st Jan 2021, 2:34 PM
Shadow
Shadow - avatar
0
Yes it worked now. Thank you for explaining me this bug.
21st Jan 2021, 2:45 PM
Himanshu Sajwan
Himanshu Sajwan - avatar