Posts tolower()
Post
Cancel

tolower()

PROBLEM DESCRIPTION

You are given a function to_lower() which takes a character array A as an argument.

Convert each character of A into lowercase characters if it exists. If the lowercase of a character does not exist, it remains unmodified. The uppercase letters from A to Z are converted to lowercase letters from a to z respectively.

Return the lowercase version of the given character array.

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {

    public char[] to_lower(char[] A) {

        for(int i=0; i<A.length; i++){

            if(A[i] >= 'A' && A[i] <= 'Z'){
                //A[i] = (char)(A[i] - ('A' - 'a')); -> This is also correct
                A[i] = (char)(A[i] ^ (1<<5)); //XOR with 2^5
            }

        }

        return A;

    }
    
}
This post is licensed under CC BY 4.0 by the author.