Java - Write code to capitalize a String and handle the null input

Requirements

Write code to capitalize a string

Handle null value, if the null value is passed in just return it back

Hint

Use if statement to check of the string is a null value.

Use Character.toUpperCase to convert the first letter to upper case.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(capitalize(str));
    }// ww w  .  jav  a 2 s .  c om

    public static String capitalize(String str) {
        if (isEmpty(str))
            return str;
        char[] cs = str.toCharArray();
        cs[0] = Character.toUpperCase(cs[0]);
        return new String(cs);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Related Exercise