0

I wrote this code at the pig latin challenge it gives the same result as the one that the callenge provides yet it doesn't pass

//example input //go over there //expected result ogay veroay heretay #include <iostream> using namespace std; int main() { string x; while(cin>>x){ int len=x.length(); char c=x[0]; x[0]=x[len]; string z="ay"; string h=x+c+z+" "; cout<<h; } return 0; } /*Is there a problem in the result type that i am returning?*/

15th Apr 2021, 7:38 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
2 Réponses
+ 2
Tricky bug you have there. You do: x[0] = x[len]; which, for reasons, will always set `x[0]` to zero. That is, not the letter '0', but the number 0 (also called null). null does not show up as anything in the output but rest assured it's still part of your string. Try this instead: string h = x.substr(1) + c + z + " "; the .substr(1) will cut away the first character. Now you probably have another hidden problem, an extra space at the end of the sentence, but I'll leave that for you to figure out.
15th Apr 2021, 10:50 PM
Schindlabua
Schindlabua - avatar
+ 1
Thx for explaining 😁, this is the first time that i encounter this type of problem so it kind of bugged me i will read more about what causes it,i appreciate the help thanks again
16th Apr 2021, 2:37 AM
Dareen Moughrabi
Dareen Moughrabi - avatar