+ 2

CamelCase to snake_case problem [SOLVED]

Did any of you guys try the challenge where one is to write a program which converts camel to snake case? I wrote something in c++ and all the visible test cases worked fine and I put it through an external compiler which had no issues either. But two of the invisible test cases failed and I‘ve got no idea why. Do you have an idea? https://code.sololearn.com/cKlkyQX6x7SJ/?ref=app

13th Mar 2022, 1:15 AM
Leon Stanek
Leon Stanek - avatar
14 ответов
+ 3
Cheesus, half way through giving it up I decided to make it work for single char strings and strings of only lower case chars. The latter was the solution. But can a string of only lower case letters really be called a CamelCase?
13th Mar 2022, 3:03 AM
Leon Stanek
Leon Stanek - avatar
+ 1
The company you are working for is refactoring its entire codebase. It's changing all naming conventions from camel to snake case (camelCasing to snake_casing). Every capital letter is replaced with its lowercase prefixed by an underscore _, except for the first letter, which is lowercased without the underscore, so that SomeName becomes some_name. Task: Write a program that takes in a string that has camel casing, and outputs the same string but with snake casing. Input Format: A string with camelCasing. Output Format: The same string but with snake_casing. Sample Input: camelCasing Sample Output: camel_casing
13th Mar 2022, 1:31 AM
HungryTradie
HungryTradie - avatar
+ 1
Using your code : input... "HelloWorld", outputs.... "_hello_world". Underscore at the beginning, i don't think that's correct. Make use of the isupper() from the "cctype" header file. https://www.cplusplus.com/reference/cctype/
13th Mar 2022, 1:32 AM
rodwynnejones
rodwynnejones - avatar
+ 1
Sorry, called it off too early. Fixed the code to make sure the first letter didn‘t get an underscore and ran tests. Code works but the third test case still fails. Read the task again but couldn‘t find the problem.
13th Mar 2022, 1:48 AM
Leon Stanek
Leon Stanek - avatar
+ 1
Try testing with CAMELCASECHALLENGE
13th Mar 2022, 1:52 AM
HungryTradie
HungryTradie - avatar
+ 1
Unnecessarily long string of upper case chars work now, test case 3 doesn‘t.
13th Mar 2022, 2:01 AM
Leon Stanek
Leon Stanek - avatar
+ 1
But I‘m fairly sure it was the longer string of lower case chars which was the issue and I really don‘t think anybody would call that a camel. One thing one can be sure about though is that people in general and managements in particular make strange decisions and can‘t be bothered to follow them themselves. Leaves a lot of wiggle room
13th Mar 2022, 2:49 PM
Leon Stanek
Leon Stanek - avatar
+ 1
import java.util.Scanner ; public class Program { public static String camelToSnake(String value) { final char[] name = value.toCharArray(); final StringBuilder builder = new StringBuilder(); for (int i = 0; i < name.length; i++) { if (Character.isUpperCase(name[i]) || name[i] == '.' || name[i] == '
#x27;) { if (i != 0 && name[i - 1] != '.' && name[i - 1] != '
#x27;) { builder.append('_'); } if (name[i] != '.' && name[i] != '
#x27;) { builder.append(Character.toLowerCase(name[i])); } } else { builder.append(name[i]); } } return builder.toString(); } public static void main(String[] args) { Scanner s=new Scanner (System .in); String str =s.nextLine(); System .out.println (camelToSnake (str)); } }
21st Oct 2022, 5:55 PM
Ionut Piturlea
Ionut Piturlea - avatar
0
Try testing with CamelCase and see if your output is camel_case or something slightly different. {Edit: if your question is solved, please update your original post to start with [Solved]. Have you found the Q&A search bar, and found the FAQ?} https://www.sololearn.com/discuss/1316935/?ref=app https://www.sololearn.com/discuss/333866/?ref=app
13th Mar 2022, 1:33 AM
HungryTradie
HungryTradie - avatar
0
Got it. Can‘t read. Case closed.
13th Mar 2022, 1:35 AM
Leon Stanek
Leon Stanek - avatar
0
Never hard code any numbers, children ( -.-)
13th Mar 2022, 1:54 AM
Leon Stanek
Leon Stanek - avatar
0
Try testing with C
13th Mar 2022, 2:04 AM
HungryTradie
HungryTradie - avatar
0
I just looked at my code for that challenge, looks like I also thought the original variable may have contained an underscore as the first character. {Edit: mine was written in C with pointers and malloc, so it would work but wouldn't be the right/correct way to do it in Cpp} {EDIT2: Sample Input: 0: camelCasing 1: functionalNaming 2: thisDoesAThing 3: 4: (is 24 characters long) 5: (has a leading underscore) Sample Output: 0: camel_casing 1: functional_naming 2: this_does_a_thing } {EDIT3: my code would not process numbers, so only alphas and underscore in the test cases}
13th Mar 2022, 2:14 AM
HungryTradie
HungryTradie - avatar
- 1
I think the story that contains the challenge justifies the inclusion of a single character variable. You are the dev trying to implement a strange decision/instruction by management... Well done for persevering, and thanks for the house keeping ✓
13th Mar 2022, 5:08 AM
HungryTradie
HungryTradie - avatar