Camel Case 4

Sort by

recency

|

532 Discussions

|

  • + 0 comments

    I dont know what is wrong with this code. its give correct output still test cases are getting failed. can somebody help me with that. code in JSI don't know what is wrong with this code. It's giving the correct output, yet the test cases are failing. Can somebody help me with that? The code is in JS.

    function processData(input) { input = input.split('\n');

    // Helper function to combine words into camelCase
    function combineWords(type, name) {
        let words = name.split(' ');
    
        // Capitalize the first letter of each word (except for the first one in the case of methods/variables)
        let camelCaseName = words[0].toLowerCase() + words.slice(1)
            .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
            .join('');
    
        // Handle method, variable, and class naming rules
        if (type === 'M') {
            return camelCaseName + '()';  // Method name ends with parentheses
        } else if (type === 'V') {
            return camelCaseName;  // Variable name is just camelCase
        } else if (type === 'C') {
            return camelCaseName.charAt(0).toUpperCase() + camelCaseName.slice(1);  // Class name starts with uppercase
        }
    }
    
    // Helper function to split camelCase name into space-delimited words
    function splitCamelCase(name) {
        return name.split(/(?=[A-Z])/).join(' ').toLowerCase().replace('()', '');
    }
    
    // Process each line of input
    input.forEach(line => {
        let [operation, type, name] = line.split(';');
    
        // Perform split operation
        if (operation === 'S') {
            const splitName = splitCamelCase(name);
            console.log(splitName);
        }
        // Perform combine operation
        else if (operation === 'C') {
            const combinedName = combineWords(type, name);
            console.log(combinedName);
        }
    });
    

    }

  • + 0 comments

    I dont know whats is wrong its is cause i am using TypeScript I Think My Output Exaly Same

    'use strict';
    
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    let inputString: string = '';
    let inputLines: string[] = [];
    let currentLine: number = 0;
    process.stdin.on('data', function(inputStdin: string): void {
        inputString += inputStdin;
    });
    
    process.stdin.on('end', function(): void {
        inputLines = inputString.split('\n');
        inputString = '';
        main();
    });
    
    function readLine(): string {
        return inputLines[currentLine++];
    }
    
    function camelCase(word:string):string {
       
      let result =  word.replace(/;/g,'');
      let code = result.slice(0,2);
      result = result.substring(2);
     
      
      if(code.toUpperCase().includes('M')){
        if(result.includes('()')){
            result = result.replace('()','');
        }
        else{
            result = result + '()' + '';
        }
      }
    
      if(code[1].toUpperCase().includes("C")){
    if(result === result.toLowerCase()){
        result = result.replace(/\b[a-z]/g, (match) => match.toUpperCase());
    } else {
        result = result.replace(/\b[A-Z]/g, (match) => match.toLowerCase());
    }
    
       
      }
     
      if(code[0].toUpperCase().includes("C")){
        let newRes = "";
        let capitalNext = false;
        for(let char = 0;char < result.length;char++){
            let a = result[char];
            if(a == " "){
                capitalNext = true;
            }
            else{
                if(capitalNext){
                    capitalNext = false;
                  a = a.toUpperCase()
                }
                newRes += a;
            }
        }
        result = newRes;
      }
      
        if(code.toUpperCase().includes('S')){
      result = result.replace(/([A-Z])/g," $1").toLowerCase();
      }
        return( result);
    }
    
    
    function main() {
      while (currentLine < inputLines.length) {
            const data = readLine();
            if (data) {
                console.log(camelCase(data));
            }
        }
    }
    
  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
    
    
       while(sc.hasNextLine()){
       String ss=sc.nextLine();
       char op =ss.charAt(0);
       char ot=ss.charAt(2);
    
       StringBuilder result = new StringBuilder();
       if(op=='S'){
    
    
            for(int i=4;i<ss.length();i++){
                char currentChar = ss.charAt(i);
                if(Character.isUpperCase(currentChar) && i>4){
                    result.append(" ");
                }
    
                result.append(Character.toLowerCase(currentChar));
    
             }   
    
        if(ot=='M' && ss.charAt(ss.length()-1)==')'){
            result.delete(ss.length()-5, ss.length()-3);
            result.toString().toLowerCase();
        }
    
    
       System.out.println(result);
    }
    
    else if(op=='C'){
        int j=0;
        for(int i=4;i<ss.length();i++){
                char currentChar = ss.charAt(i);
    
                if(currentChar==' '){
                    j=i+1;
                     continue;                  
                }
                if(j==i){
                    result.append(Character.toUpperCase(currentChar));
                    continue;
                }
                result.append(currentChar);
    
             }
    
             if(ot=='M'){
                result.append('(');
                result.append(')');
    
             }
             if(ot=='C'){
               String  res=result.substring(0,1).toUpperCase() +result.substring(1);
             System.out.println(res);
             continue;
             }  
    
             System.out.println(result);         
    }  
    }
    }}
    
  • + 1 comment

    can anyone tell me whats wrong here?#Java

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

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scanner =new Scanner(System.in);
    
        while(scanner.hasNextLine()){
            String input=scanner.nextLine();
            //System.out.println(input);
    
            char dec1=input.charAt(0);
            char dec2=input.charAt(2);
            int last=input.length()-1;
            char lastIndex=input.charAt(last);
            //int lastBefore=last-1;
    
    
            StringBuilder result= new StringBuilder();
    
            if(dec1=='S'){
                for(int i=4;i<input.length();i++){
                    char currentChar = input.charAt(i);
                    if(Character.isUpperCase(currentChar) && i>0){
                        result.append(" ");
                    }
                    result.append(currentChar);
    
    
                }
    
                if(dec2=='M' && lastIndex==')'){
                    result.delete(result.length()-2, result.length());
                    result.toString().toLowerCase();
                    System.out.println(result);
                }else if(dec2=='V' && result.toString().contains(" ")){
                    int index= result.indexOf(" ");
    
                    if(index<result.length()){
                        //setCharAt(index, character)
                        result.setCharAt(index+1,Character.toLowerCase(result.charAt(index+1)));
                    }
                    System.out.println(result.toString());
                }else if(dec2=='C' && result.toString().contains(" ")){
    
                    if(result.charAt(0)==' '){
                        result.deleteCharAt(0);
                        System.out.println(result.toString().toLowerCase());
                    }else{
                        System.out.println(result.toString().toLowerCase());
                    }
                }
    
                }else{
                    StringBuilder newBuild=new StringBuilder(input);
    
                    if(dec2=='V' && newBuild.toString().contains(" ")){
                        newBuild.delete(0,4);
                        for(int i=0; i<newBuild.length();i++){
                            if(newBuild.charAt(i)==' ' && Character.isLowerCase(newBuild.charAt(i+1))){
                                newBuild.deleteCharAt(i);
                                newBuild.setCharAt(i,Character.toUpperCase(newBuild.charAt(i)));
                            }
                        }
                        System.out.println(newBuild.toString());
                    }else if(dec2=='C'){
                        newBuild.delete(0,4);
                        if(Character.isLowerCase(newBuild.charAt(0))){
                            newBuild.setCharAt(0,Character.toUpperCase(newBuild.charAt(0)));
                        }
                        for(int i=0; i<newBuild.length()-1;i++){
                            // char ch=newBuild.charAt(i);
                            if(newBuild.charAt(i)==' ' && Character.isLowerCase(newBuild.charAt(i+1))){
                                newBuild.deleteCharAt(i);
                                newBuild.setCharAt(i, Character.toUpperCase(newBuild.charAt(i)));
    
                            }
                        }
                        System.out.println(newBuild.toString());
                    }else if(dec2=='M'&&newBuild.toString().contains(" ")){
                        newBuild.delete(0,4);
                        for(int i=0; i<newBuild.length()-1; i++){
                            if(newBuild.charAt(i)==' ' && Character.isLowerCase(newBuild.charAt(i+1))){
                                newBuild.deleteCharAt(i);
                                newBuild.setCharAt(i, Character.toUpperCase(newBuild.charAt(i)));
    
                            }
                        }newBuild.append("()");
                        System.out.println(newBuild.toString());
                    }
    
        }
    
            }
    
    
        }
    
    }
    
  • + 1 comment

    The input data ends a line with "\r\n", so you would need to strip that '\r' if your language's readline doesn't do that.