NamingConvention
SRM 638 · 2014-08-25 · by cgy4ever
SRM 638 · 2014-08-25 · by cgy4ever · String Manipulation
Problem Statement
Problem Statement
In most programming languages variable names cannot contain spaces.
If we want a variable name that consists of two or more words, we have to encode the spaces somehow.
In this problem, we will look at two ways of doing so: Snake Case and Camel Case.
When using Snake Case, we just replace each space by an underscore ('_').
When using Camel Case, we capitalize the first letter of each word except for the first one, and then we remove all spaces.
For example, suppose that we want to declare a variable called "good morning world" (quotes for clarity). In Snake Case, we would write this variable as "good_morning_world", while in Camel Case it would be "goodMorningWorld".
You are given aString variableName.
This String contains a valid variable name written in Snake Case.
Return the same variable name in Camel Case.
For example, suppose that we want to declare a variable called "good morning world" (quotes for clarity). In Snake Case, we would write this variable as "good_morning_world", while in Camel Case it would be "goodMorningWorld".
You are given a
Constraints
- variableName will contain between 1 and 50 characters.
- Each character of variableName will be 'a'-'z' or '_'.
- The first and last character of variableName will not be '_'.
- variableName will not contain two consecutive underscores ('_').
Examples
0)
"sum_of_two_numbers" Returns: "sumOfTwoNumbers"
We have 4 words: "sum", "of", "two", "numbers". So the answer is "sum" + "Of" + "Two" + "Numbers".
1)
"variable" Returns: "variable"
Note that if we have only 1 word, then the varaible name will remain same.
2)
"t_o_p_c_o_d_e_r" Returns: "tOPCODER"
3)
"the_variable_name_can_be_very_long_like_this" Returns: "theVariableNameCanBeVeryLongLikeThis"
4)
"a" Returns: "a"
Submissions are judged against all 21 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class NamingConvention with a public method string toCamelCase(string variableName) · 21 test cases · 2 s / 256 MB per case