To Lower Case LeetCode Solution - The Coding Shala

Home >> LeetCode >> To Lower Case

 In this post, we will learn how to solve LeetCode's To Lower Case Problem and will implement its solution in Java.

To Lower Case Problem

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:
Input: "Hello"
Output: "hello"

Practice this problem on LeetCode(Click Here).

To Lower Case Java Solution

Approach 1

We can compare the ASCII value of char and if it is within the capital range then add 32 and change it to char.

32 is the difference between capitals and small char ASCII values.

Java Program: 

class Solution {
    public String toLowerCase(String str) {
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<str.length(); i++) {
            int ch = (int) str.charAt(i);
            if(ch >= 65 && ch <= 90) {
                sb.append((char) (ch+32));
            } else sb.append((char) ch);
        }
        return sb.toString();
    }
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala