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.

HackerRank Camel Case 4 problem solution


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



Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {     
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String input = scanner.nextLine();
            String[] split = input.split(";");
            char op = split[0].charAt(0);
            char type = split[1].charAt(0);
            String words = split[2];
            char[] wordsArr = words.toCharArray();
            String output = "";
            
            if (op == 'S') {
                for (char ch: wordsArr) {
                    if (Character.isUpperCase(ch)) {
                        output += " " + Character.toLowerCase(ch);
                    } else if (ch != '(' && ch != ')') {
                        output += ch;
                    }
                }
            } else if (op == 'C') {
                for (int i = 0; i < wordsArr.length; i++) {
                    char currentChar = wordsArr[i];
                    if (currentChar != ' ') {
                        if (i > 0 && wordsArr[i - 1] == ' ') {
                            currentChar = Character.toUpperCase(currentChar);
                        }
                        output += currentChar;
                    }
                }
                if (type == 'M') {
                    output += "()";
                } else if (type == 'C') {
                    output = output.substring(0, 1).toUpperCase() + output.substring(1);
                }     
            }
            
            System.out.println(output.trim());
        }
    }
}


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;
}