+ 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
14 Answers
+ 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?
+ 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
+ 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/
+ 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.
+ 1
Try testing with CAMELCASECHALLENGE
+ 1
Unnecessarily long string of upper case chars work now, test case 3 doesnât.
+ 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
+ 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));
}
}
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
0
Got it. Canât read. Case closed.
0
Never hard code any numbers, children ( -.-)
0
Try testing with C
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}
- 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 â