1

In codewars I have completed a kata using for loop with 15 lines of code, some other person has completed it with just 7 lines. Could anybody explain the code?

public class CamelCase {
    public static String cAmEl(final String yourName) {
        final int length = yourName.length();
        final StringBuilder cAmEl = new StringBuilder(length);
        boolean upper = true;
        for (int i = 0; i < length; i++, upper ^= true) {
            final char c = yourName.charAt(i);
            cAmEl.append(upper ? toUpperCase(c) : toLowerCase(c));
        }
        return cAmEl.toString();
    }
}

The code converts every alternate character of a string to uppercase (starting with an uppercase character). For example: test becomes TeSt.

I am unable to understand this part

cAmEl.append(upper ? toUpperCase(c) : toLowerCase(c));
Michu93
  • 5,058
  • 7
  • 47
  • 80
krishna
  • 65
  • 1
  • 6

3 Answers3

4
cAmEl.append(upper ? toUpperCase(c) : toLowerCase(c))

This line appends the next character to a StringBuilder. It appends toUpperCase(c) if upper is true and toLowerCase(c) if upper case is false. It uses the ternary operator, which is similar to :

if (upper)
    cAmEl.append (toUpperCase(c));
else
    cAmEl.append (toLowerCase(c));

Since upper alternates between true and false (that's what the XOR operator does here - upper ^= true), this means that the output characters will alternate between upper case and lower case, as required.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • While it is true that `bool ? foo : bar` is ternary operator (just like `++` is unary and `a * b` is binary) just because Java doesn't have more ternary operators people are using "ternary" as if it is was proper name of this operator. But since we are not guaranteed that in future Java will not add more ternary operators, it is better to call it by its proper name which is "[conditional operator](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25)". – Pshemo Jun 20 '15 at 13:59
1

Not a direct answer, but you can do it in one (quite long) line of code:

return yourName.length() <= 1 ? yourName.toUpperCase() : "" + Character.toUpperCase(yourName.charAt(0)) + Character.toLowerCase(yourName.charAt(1)) + cAmEl(yourName.substring(2)); 

or splitting it up a bit:

return yourName.length() <= 1 ? yourName.toUpperCase() : 
    "" + Character.toUpperCase(yourName.charAt(0)) +  
         Character.toLowerCase(yourName.charAt(1)) + 
         cAmEl(yourName.substring(2));
rghome
  • 8,529
  • 8
  • 43
  • 62
1

The part you don't understand is a ternary. Ternary contains of three parts. part one: Condition

upper

part two: True statement

toUpperCase(c)

part three: False Statement

toLowerCase(c)

As you can see in for statement upper will be XOR to literal value true.

for (int i = 0; i < length; i++, upper ^= true)

So in each iterate of the for statement value of upper variable will be reverse, so true statement and false statement in ternary will be called.

A.v
  • 734
  • 5
  • 26