+ 1

Camel to Snake code coach , Can anyone help me find what's wrong with my code? C++ (solved)

I tried to run my code but for some reason failed test case 5 Problem: Camel to Snake Code: #include <iostream> #include <cctype> using namespace std; int length; int main() { //camelCase to snake_case char input[50]; cin>>input; for (int i=0;i<50;i++){ if (input[i]==NULL){ break; } length++; } //le convert for (int i=0;i<length;i++){ if (isupper(input[0])){ i++; } else if (isupper(input[i])){ input[i]=tolower(input[i]); cout<<"_"; } cout<<input[i]; } }

17th Jan 2023, 2:30 AM
Skiidedum
Skiidedum - avatar
12 Réponses
+ 4
Hi Nat, Don’t forget your using cpp, prefer std::string over char arrays - let it do the memory management for you (instead of guessing the char size of 50, it will allocate only the memory you need). You also have the added functionality of its member functions, added safety, iterators etc. There’s many ways you can do this, if you really wanted to use a char array you could for example use pointers and size/allocate the char array dynamically at runtime. Its probably easier to work through an example, so take a look at this…it’s a bit simpler and uses std::string. #include <iostream> #include <string> using namespace std; int main() { std::string camel, snake, sTemp; getline(std::cin, camel); for (int i = 0; i < camel.length(); i++) { if (std::isupper(camel[i])) { sTemp = std::tolower(camel[i]); if (i != 0) { snake = '_' + sTemp; } else { snake = sTemp; } camel.replace(i, 1, snake); } } std::cout << camel; return 0; }
17th Jan 2023, 3:55 AM
DavX
DavX - avatar
+ 4
DavX Of course everyone is entitled to their opinion. And mine is that it's better to help someone fix their own code first. If they are asking how they can further refine a working code that's a different story.
17th Jan 2023, 4:19 AM
Scott D
Scott D - avatar
+ 2
Mirielle You wrote: "If the first letter of the input string is uppercase, it should be converted to lowercase and a "_" should be added before it, but the code skips this step." Actually this is incorrect. The Code Coach description states: "except for the first letter, which is lowercased without the underscore, so that SomeName becomes some_name". Nat Your first "if" statement needs to be modified so that if the first letter isupper it becomes islower without adding the underscore. So the i++ also needs to be removed.
17th Jan 2023, 3:40 AM
Scott D
Scott D - avatar
+ 2
Mirielle I understand, I was simply pointing out the issue as I recognized the specific Code Coach having done it myself, it was only intended as an observation to help Nat and clarify the issue to you as well, it was not meant to sound critical. Perhaps I could have worded it better, pointing the directions out without mentioning you, for that I apologize, just wasn't thinking.
17th Jan 2023, 4:04 AM
Scott D
Scott D - avatar
+ 2
Scott, It's just a habit of using the correct namespace (and not a bad one to have to boot!). For new comers it really helps in demonstrating which methods are from the standard library. Plus clearly the user has grasped the concept, use of loops etc...I think a working example with strings to work through was appropriate.
17th Jan 2023, 4:16 AM
DavX
DavX - avatar
+ 2
Thank you all for answering! It's very much appreciated and I learned new things as well! I also fixed the code and completed the code coach, so thank you all for the help!
17th Jan 2023, 9:27 AM
Skiidedum
Skiidedum - avatar
+ 1
DavX When using namespace std all those extra std:: are unnecessary and simply make the code look unnecessarily complicated. Also it is generally prefered to give hints and tips, not full code.
17th Jan 2023, 3:59 AM
Scott D
Scott D - avatar
0
Broo Incloud <studio.h>
17th Jan 2023, 4:00 PM
Mosali Raju Jagan
Mosali Raju Jagan - avatar
0
Include type.c id by default
18th Jan 2023, 2:53 PM
Andrei92