+ 3

Pig Latin challenge

Update-2/13/2020. Solved, but needs cleaned up using advise from comments. Code link has been updated as well. Next im going to work on understanding the use of the "return", and eliminate the global variable. If anyone wants to make the adjustment and post a link, that would be awesome! Ive worked on this for a week, and need help and critiqued. The code works, but doesn't pass the challenges. I believe the problem is because of the extra white space in the beginning, in between words, and end. Let me know where im going wrong, and please PLEASE, critique me on what I could have done better with this code. If anyone says "use pointers", please give an example. I would have used pointers if I understood them better, and im currently working on that topic. Thanks all!!https://code.sololearn.com/cFFeN1k4AO1B/#cpp

12th Feb 2020, 7:07 PM
Beau Tooley
Beau Tooley - avatar
9 Answers
+ 3
+ the good thing about your code is, it's commented and the variables are well named👍. - you don't need <string.h> - since the function only modifies the global var output. it doesn't have to return anything. could be void instead of string and remove return x; * you could do everything inside the function and return the result. - there is some null chars somewhere I made your code pass by removing : cout << output ; and replace it with : for(int i = 0;i < output.length();i++){ if(output[i] == NULL){ continue; } putchar(output[i]); }
12th Feb 2020, 8:23 PM
Bahha🐧
Bahha🐧 - avatar
+ 2
Salam I think you don't need a global variable for such task you can make your code simpler. See the changes I've made, you might like it. https://code.sololearn.com/ccqHnuolE74m/?ref=app
14th Feb 2020, 7:55 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 1
"int last_letter=x.length();" "x[0]=x[last_letter];" I don't think this does what you think it does. I'll let you have a try at it first in order to figure it out. Why are you returning x in convert_to_pigLatin when you're not using it when you call the function? Don't you think you can scrap the global variable and return the converted string instead? The challenge doesn't care about a trailing whitespace here. String has a function called "pop_back" which you could utilize if you do, though. And no, pointers are probably not necessary here. :)
12th Feb 2020, 7:47 PM
Dennis
Dennis - avatar
+ 1
thanks for the responses, lots of good info. Im also now just noticing that im switching the first and last char, and adding "ay". You are only supposed to move the first letter to the end and add "ay". No idea where i got that idea. Crazy how tunnel vision you can get on code when you are trying to nail down the specifics. Im going to leave this discussion open, and completely re-tackle the code and figure out how to efficiently move the first char to the end without having an empty space yet again:)
12th Feb 2020, 8:43 PM
Beau Tooley
Beau Tooley - avatar
+ 1
Rafik, That's looks awesome. The implementation looks great, and I also like how you combined the declaration of the input and output to a single line. That cleaned up the code very nice.
14th Feb 2020, 9:09 PM
Beau Tooley
Beau Tooley - avatar
+ 1
I have a code that also work, and I don't need to include the string header, thanks to your code Beau Tooley it's similar with yours, but with a little difference https://code.sololearn.com/c8M2uBhpFu36/?ref=app
15th Nov 2020, 12:13 PM
Imam Fauzi
Imam Fauzi - avatar
0
for(int i=0;i<=input_length; i++) I think problem may be by this.... Is not there only <, (instead of <=) Line 32.
12th Feb 2020, 7:50 PM
Jayakrishna🇮🇳
Jayakrishna🇮🇳 - avatar
0
[user id=3800510]Dennis[/user] , i see what youre saying. looks like i had the thinking in my head, but didnt translate that very well. im gonna redo that part and update the code. good find!!! guess thats the problem with well defined variables, they convince you they are doing what they say:)
12th Feb 2020, 8:15 PM
Beau Tooley
Beau Tooley - avatar
0
You should always try to do the same thing in the future and you will be able to be creative in writing code. I' glad you like it.
14th Feb 2020, 9:13 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar