Camel Case 4 Hacker Rank Problem Solution

Camel Case 4 Hacker Rank Problem Solution

In this HackerRank Camel Case 4 problem-solution Camel Case is a naming style common in many programming languages. In Java, method and variable names typically start with a lowercase letter, with all subsequent words starting with a capital letter (example: startThread). Names of classes follow the same pattern, except that they start with a capital letter (example: BlueCar).

Your task is to write a program that creates or splits Camel Case variable, method, and class names.

Input Format

  • Each line of the input file will begin with an operation (S or C) followed by a semi-colon followed by M, C, or V followed by a semi-colon followed by the words you’ll need to operate on.
  • The operation will either be S (split) or C (combine)
  • M indicates method, C indicates class, and V indicates variable
  • In the case of a split operation, the words will be a camel case method, class or variable name that you need to split into a space-delimited list of words starting with a lowercase letter.
  • In the case of a combine operation, the words will be a space-delimited list of words starting with lowercase letters that you need to combine into the appropriate camel case String. Methods should end with an empty set of parentheses to differentiate them from variable names.

Output Format

  • For each input line, your program should print either the space-delimited list of words (in the case of a split operation) or the appropriate camel case string (in the case of a combine operation).

Sample Input

S;M;plasticCup()

C;V;mobile phone

C;C;coffee machine

S;C;LargeSoftwareBook

C;M;white sheet of paper

S;V;pictureFrame

Sample Output

plastic cup

mobilePhone

CoffeeMachine

large software book

whiteSheetOfPaper()

picture frame

Explanation

  • Use Scanner to read in all information as if it were coming from the keyboard.
  • Print all information to the console using standard output (System.out.print() or System.out.println()).
  • Outputs must be exact (exact spaces and casing).

Camel Case 4 Hacker Rank Problem Solutions

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in JavaScript

function splitOperation(input) {
return input.replace(/[^\w]/g, '').replace(/([a-z])([A-Z])/g, '$1 $2').toLowerCase()
}

function titleCase(input) {
return `${input[0].toUpperCase()}${input.slice(1)}`
}

function combineOperation(input, identifier) {
if (identifier === 'M') {
return combineOperation(input, 'V') + '()'
}
if (identifier === 'C') {
return input
.replace(/(\w+)/g, (_, word) => titleCase(word))
.replace(/\s/g, '')
}
if (identifier === 'V') {
return input
.replace(/(\w+)/g, (_, word, offset) => offset > 0 ? titleCase(word) : word)
.replace(/\s/g, '')
}
return input
}

function processData(input) {
const lines = input.split(/\r?\n/)

lines.forEach(function (line) {
const [operation, identifier, text] = line.split(/;/)

if (operation === 'S') {
console.log(splitOperation(text))
}
if (operation === 'C') {
console.log(combineOperation(text, identifier))
}
})
}

Problem Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
string s,output;
while(getline(cin,s)){
s.erase(remove(s.begin(), s.end(), '\r'), s.end());
output="";
char opr = s[0], typ = s[2];
for(int i = 4; i<s.size();i++) {
if(opr=='C'){
if(i==4 && typ =='C'){
output+=toupper(s[i]);
continue;
}
if(s[i]==' ') continue;
if(i!=4 && s[i-1]==' '){
output+=toupper(s[i]);
continue;
}
output+=s[i];
} else {
if(s[i]=='(') break;
if(isupper(s[i]) && i!=4){
output+=" ";
output+=(char)(s[i]+32);
continue;
}
if(typ=='C' && i==4){
output+=(char)(s[i]+32);
continue;
}
output+=s[i];
}
}
if(opr=='C' && typ=='M'){
cout<<output+"()"<<endl;
} else {
cout<<output<<endl;
}
}
return 0;
}

Problem Solution in Python

import re
while True:
try:
s = input().rstrip()
sc, mcv, op = s.split(";")
if sc == "S":
if mcv == "M":
cap = op[:-2]

if mcv == "C" or mcv == "V":
cap = op

s = re.sub ("(\w)([A-Z])", r"\1 \2", cap)
print (s.lower())

if sc == "C":
cap = op.title ()
s = re.sub (r" ", r"", cap)
q = s[:1].lower() + s[1:]

if mcv == "M":
print (q+"()")

if mcv == "C":
print (s)

if mcv == "V":
print (q)

except EOFError:
break