Camel Case 4

Sort by

recency

|

548 Discussions

|

  • + 0 comments

    Longwinded Javascript solution with switchcase and regex.

        const splitInput = input.split('\r\n');   
        const words = [];
        let formattedWords;
        
        splitInput.forEach(input => {
            const code = input.slice(0,3);
            const latterString = input.slice(4);
            const splittedWords = [];
            
            switch(code){
                case 'S;V':
                case 'S;C':
                splitWords = latterString.split(/(?=[A-Z])/).map(word => word.toLowerCase());
                words.push(splitWords.join(' '));
                break;
                case 'S;M':
                splitWords = latterString.split(/(?=[A-Z])/).map(word => word.toLowerCase());
                splitWords[splitWords.length -1] = splitWords[splitWords.length -1].slice(0,-2);
                words.push(splitWords.join(' '));
                break;
                case 'C;V':
                combineWords = latterString.split(' ').map(word => word[0].toUpperCase() + word.slice(1));
                combineWords[0] = combineWords[0][0].toLowerCase() + combineWords[0].slice(1);
                words.push(combineWords.join(''));
                break;
                case 'C;C':
                combineWords = latterString.split(' ').map(word => word[0].toUpperCase() + word.slice(1));
                words.push(combineWords.join(''));
                break;
                case 'C;M':
                combineWords = latterString.split(' ').map(word => word[0].toUpperCase() + word.slice(1));
                combineWords[0] = combineWords[0][0].toLowerCase() + combineWords[0].slice(1);
                words.push(combineWords.join('')+ '()');
                break;
            }
        })
        
        formattedWords = words.join('\n');
        console.log(formattedWords);
    } 
    
  • + 0 comments

    Can someone please help why this code is failing

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    
    char makeCapitalLetter(const char c) {
        return c & (~(1<<5));
    }
    
    char makeSmallLetter(const char c) {
        return c | (1<<5);
    }
    
    
    string combineString(string raw, bool firstCapital=false, bool allCapital=true){
        stringstream ip{raw};
        string output = "";
        do {
            string word;
            ip>>word;
            
            if(allCapital) {
                word[0] = makeCapitalLetter(word[0]);
            }
            output+=word;
            
        }while(ip);
        
        if(!firstCapital) {
            output[0] = makeSmallLetter(output[0]);
        }
        return output;
    }
    
    string splitString(string raw) {
        string result = "";
        raw[0] = makeSmallLetter(raw[0]);
        for(auto c:raw) {
            if(isupper(c)) {
                result+=" ";
                result+=makeSmallLetter(c);
            }
            else {
                result+=c;
            }
        }
        return result;
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        string line;
        while(getline(cin, line)) {
            stringstream ss{line};
            char cmd, type, tmp;
            string input, result;
            ss>>cmd>>tmp>>type>>tmp;
            getline(ss, input);
            
            #if 0
            cout<<cmd<<endl;
            cout<<type<<endl;
            cout<<input<<endl<<endl;
            #endif 
            
            switch (cmd) {
                case 'S':
                {
                    switch (type) {
                        case 'M': 
                            input = input.substr(0,input.size()-2);
                        case 'C': 
                        case 'V':
                            result = splitString(input);
                            break;
                        default:
                            break;
                    }
                }
                break;
                case 'C':
                {
                    switch (type) {
                        case 'M':
                        {
                            result = combineString(input);
                            result+="()";
                        }
                        break;
                        case 'C':
                        {
                            result = combineString(input, true);
                        }
                        break;
                        case 'V':
                        {
                            result = combineString(input);
                        }
                        break;
                        default:
                        {
                            
                        }
                    }
                }
                break;
            }
            
            cout<< result<< endl;
            
        }
        return 0;
    }
    
  • + 0 comments

    there's nothing basic about this challenge

  • + 0 comments

    Here is my c# attempt, feel free to provide feedback, use or comment.

    using System.Text.RegularExpressions;

    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int n = -1;
        string input = "";
        char[] trimM = {'(', ')'};
        List<string> texts = new List<string>();
        string[] operations, separate;
        string combine = "";
        input = Console.ReadLine();
        while(input != null)
        {
            texts.Add(input);
            input = Console.ReadLine();
        }
        foreach(string s in texts){
            operations = s.Split(";");
            switch(operations[0])
            {
                case "S":
                    if(operations[1] == "M")
                        operations[2] = operations[2].Trim(trimM);
                    separate = Regex.Split(operations[2], @"(?<!^)(?=[A-Z])");
                    foreach(string word in separate)
                    {
                        if(separate.Last() == word)
                            Console.WriteLine(word.ToLower());
                        else
                            Console.Write(word.ToLower() + " ");
                    }
                    break;
                case "C":
                    separate = operations[2].Split(" ");
                    foreach(string word in separate)
                    {
                        if(word == separate.First())
                        {
                            if(operations[1] == "C")
                                combine += word[0].ToString().ToUpper() + word.Substring(1);
                            else
                                combine += word;
                        }
                        else
                        {
                            combine += word[0].ToString().ToUpper() + word.Substring(1);
                        }
                    }
                    if(operations[1] == "M")
                            combine += "()";
                    Console.WriteLine(combine);
                    combine = "";
                    break;
            }
        }
    }
    

    }

  • + 0 comments
    import sys
    while True:
        line=sys.stdin.readline().strip()
        if not line:
            break
        Input=line.split(";")
        operation=Input[0]
        Type=Input[1]
        word=Input[2]
        if operation=="S":
            res=""
            for i in word:
                if i.isupper() and not res:
                    res+=i.lower()
                elif i.isupper() and res:
                    res+=" "+i.lower()
                elif i=="("or i==")":
                    continue
                else:
                    res+=i
            print(res)
        elif operation=="C":
            String=word.split()
            res=String[0].lower()
            if Type=="C":
                res=res.capitalize()
            for i in String[1:]:
                res+=i.capitalize()
            if Type=="M":
                res+="()"
            print(res)