Break camelCase Challenge in Java

0 min read 98 words

The challenge

Complete the solution so that the function will break up camel casing, using a space between words.

Example

solution("camelCasing")  ==  "camel Casing"

Test cases

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Test
    public void tests() {
      assertEquals( "Incorrect", "camel Casing", Solution.camelCase("camelCasing"));
      assertEquals( "Incorrect", "camel Casing Test", Solution.camelCase("camelCasingTest"));
      assertEquals( "Incorrect", "camelcasingtest", Solution.camelCase("camelcasingtest"));
    }
}

The solution in Java

class Solution {
  public static String camelCase(String input) {
    String out = "";
    
    for (int i=0; i<input.length(); i++) {
      String c = Character.toString(input.charAt(i));
      if (c.equals(c.toUpperCase())) {
        out+=" ";
      }
      out+=c;
    }
    
    return out;
  }
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts