Camel Case 4

  • + 1 comment

    This works fine using Java public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
    
        while(sc.hasNextLine()){
             String line = sc.nextLine();
             String [] parts = line.split(";");
             String operation = parts[0];
             String type = parts[1];
             String text = parts[2];
    
        if(operation.equals("S")){
            String result = split(type, text);
            System.out.println(result);
        } else if(operation.equals("C")){
            String result = combine(type, text);
            System.out.println(result);
        }
    }
       sc.close();
    }
    public static String split(String type, String text){
        StringBuilder result = new StringBuilder();
        for(int i = 0; i < text.length(); i++){
            char c = text.charAt(i);
            if(Character.isUpperCase(c)){
                if(i > 0)
                result.append(" ");
            result.append(Character.toLowerCase(c));   
            } else if( c != '(' && c != ')'){
            result.append(c);
          } 
        } 
        return result.toString();
       }
    public static String combine(String type, String text){
        StringBuilder result = new StringBuilder();
        String[] words = text.split(" ");
        for(int i = 0; i< words.length; i++){
            String word = words[i];
            if(i == 0 && !type.equals("C"))
               result.append(word.toLowerCase());
            else {
                result.append(Character.toUpperCase(word.charAt(0)));
                result.append(word.substring(1).toLowerCase());
            }   
        } 
        if(type.equals("M"))
           result.append("()");
        return result.toString();
    }