Wednesday, February 3, 2016

Lowercase to Uppercase II

Lowercase to Uppercase II

Implement an upper method to convert all characters in a string to uppercase.
You should ignore the characters not in alphabet.

Example
Given "abc", return "ABC".
Given "aBc", return "ABC".

Given "abC12", return "ABC12".


public class Solution {
    /**
     * @param str a string
     * @return a string
     */
    public String lowercaseToUppercase2(String str) {
        // Write your code here
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            result = result + toStr(str.charAt(i));
        }
        return result;
    }
    
    private char toStr(char c) {
        if (c >= 'a' && c <='z') {
            return (char) (c + 'A' - 'a');
        }
        return c;
    }
    
}