0

Pig Latin c++

Hi! The code works but I feel like I wrote a lot of code to something that probably can be solved in a easier way, if anyone knows how to solve it with less code I'll really appreciate it https://code.sololearn.com/cgB2RtYvFXcM/?ref=app

28th Oct 2021, 3:20 AM
Itzel Medina
Itzel Medina - avatar
6 Answers
+ 2
What about: #include <iostream> #include using namespace std; int main() { for(string p; cin>>p;) cout << p.substr(1) << p[0] << "ay "; } Anyway, your code has some mistakes in it, I think you should be more concerned about those
28th Oct 2021, 4:54 AM
Angelo
Angelo - avatar
+ 1
This is my code. #include <iostream> #include #include using namespace std; int main() { string input; getline(cin, input); vector col; for(size_t i = 0, g = 0; i < input.length()+1; ++i) { if(input[i] == ' ' || i==input.length()) { col.push_back(input.substr(g, i-g)); g = i+1; } } for(auto& str : col) { cout << str.substr(1, str.length()-1) << str[0] << "ay "; } return 0; }
28th Oct 2021, 4:50 AM
CarrieForle
CarrieForle - avatar
+ 1
Itzel Medina line 12: sizeof(palabra) is not the length of the string line 15: palabra[j+2]!=0 will not count single character words at the end of the sentence line 27: palabras[i] not checking for i<15 may lead to undefined behavior line 29-36: idem as above (line27) line 31: j<15, only the first 15 char of every word are shifted
28th Oct 2021, 8:20 AM
Angelo
Angelo - avatar
0
Angelo Oh really? Like what mistakes? Thanks btw
28th Oct 2021, 6:26 AM
Itzel Medina
Itzel Medina - avatar
0
CarrieForle thanks! πŸ™
28th Oct 2021, 6:28 AM
Itzel Medina
Itzel Medina - avatar
0
Angelo thanks for the feedback! (I'm not sure if I said right, English isn't my main language πŸ˜…)
28th Oct 2021, 4:06 PM
Itzel Medina
Itzel Medina - avatar