Given string X, convert it to Java Style string, as defined below:

  • Strings may only contain numbers and letters(signs are not allowed)
  • Strings must not start with numbers
  • Strings must start with Capital letter
  • Strings may not contain two capital letters in a raw.

As for conversion you may perform the next actions

  • Replace capital letter with lower case letter and vise versa.
  • Remove character

A good conversion is defined as follow:

Minimum actions been performed while achieving the next goals:

  • all the sequences of capital letters only kept the first letter as capital.
  • if a letter has a character in place before it, and the character is not a letter nor a number, then the letter becomes capital.
  • in case that after a good conversion there is a contradiction with the style, good conversion will be applied again on the result of previous conversion.
  • If there is more then one way to apply good conversion, than conversion A is considered to be better then conversion B if conversion A has more capital letters then B.


  • String x


  • Best conversion Java Style String

I/O examples

X                Output

'ab<>c'          'AbC'
'a<>b<>c'        'Abc' // conversion been applied twice. ABC -> Abc
'a<>bc'          'Abc' // conversion been applied twice. ABc -> Abc
'HI<>man'        'HiMan'
''               ''
'its**cOOL'      'ItsCool'
'28and*2fun'     'And2fun' // conversion been applied twice. and2fun -> And2fun

In this example strings been wrapped with '' sign.


  • The shortest program wins, as determined by the number of bytes of source code.
  • \$\begingroup\$ What about underscores? Java allows underscores in names. \$\endgroup\$ – Doorknob Mar 30 '15 at 21:52
  • 1
    \$\begingroup\$ @Ilya_Gazman Yes, but AbC does not contain two capitals in a row. \$\endgroup\$ – Doorknob Mar 30 '15 at 21:52
  • \$\begingroup\$ @Doorknob i was afraid it will be to confusing so I did not allow it. My definition is to fallow. \$\endgroup\$ – Ilya Gazman Mar 30 '15 at 21:53
  • 1
    \$\begingroup\$ Would 28and*2Fun (note the capital F) become And2Fun? Also would its**coOL (one lowercase o) become ItsCoOl? \$\endgroup\$ – Martin Ender Mar 30 '15 at 22:11
  • 5
    \$\begingroup\$ Without more detail about the interactions between the rules, they seem to contradict each other and some of the test cases. E.g. 'minimum actions been performed' would imply that no letter should be converted to a capital except the first one (required for conformance with the rules for valid output). May I point you at the sandbox? \$\endgroup\$ – Peter Taylor Mar 31 '15 at 7:31

Java 551 490 bytes

public class A{public static void main(String...a){String b="";for(int i=0;i<2;i++){String[]c=a[0].split("[^a-zA-Z0-9]");if(c[0].length()>0&&Character.isDigit(c[0].charAt(0)))c[0]=c[0].replaceFirst("[0-9]+","");b="";for(String d:c){boolean e=false;String f="";byte[]g=d.getBytes();for(byte h:g){if(e){if(Character.isLowerCase(h))e=false;else h+='a'-'A';}else if(!Character.isLowerCase(h))e=true;f+=(char)h;}d=f;if(d.length()>0)b+=StringUtils.capitalize(d);}a[0]=b;}System.out.println(b);}}

Ungolfed Code:

public class A{
    public static void main(String...args){
        String input = args[0];

        String className = "";
        for (int i = 0; i < 2; i++) {
            String[] names = input.split("[^a-zA-Z0-9]");
            if(names[0].length() > 0 && Character.isDigit(names[0].charAt(0))){
                names[0] = names[0].replaceFirst("[0-9]+", "");
            className = "";
            for (String value : names) {
                boolean toLowerCase = false;
                String newValue = "";
                byte[] bytes = value.getBytes();
                for (byte aByte : bytes) {
                        if (Character.isLowerCase(aByte)){
                            toLowerCase = false;
                            aByte += 'a' - 'A';
                    else if (!Character.isLowerCase(aByte)){
                        toLowerCase = true;
                    newValue += (char)aByte;

                value = newValue;
                if(value.length() > 0){
                    className += StringUtils.capitalize(value);
            input = className;


| improve this answer | |

Haskell 316 bytes

Not fully golfed, may spend time to golf a bit more if others come and join the game.

import Data.Char
data T = C | S | X
conv t [] = []
conv t (c:cs)
  |isLower c = case t of X -> toUpper c : conv C cs; _ -> c : conv S cs
  |isUpper c = case t of C -> toLower c : conv X cs; _ -> c : conv C cs
  |isDigit c = c : conv S cs
  |otherwise = conv X cs
main = interact $ conv X . dropWhile isDigit . conv X

The C, S, X, data = T = C | S | X stands for Capital, Small, Other.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.