Convert the given string into lowercase in Java


This Java program converts all uppercase letters in a given string to lowercase.

  • The program starts by initializing a StringBuilder object a with the string "ABCD", and it is printed using System.out.println("Original Input : "+a).
  • A for loop is used to iterate through the characters of the string. If the character is an uppercase letter (its ASCII value is between 65 and 90), it is converted to lowercase by adding 32 to its ASCII value and then setting the new character using the setCharAt() method of the StringBuilder object.
  • Finally, the updated string is printed using System.out.println("Lowercase Output: "+a).

Source Code

public class lowercase {
    public static void main(String args[]) {
        //Program to Convert string to LowerCase
        StringBuilder a = new StringBuilder("ABCD");
        System.out.println("Original Input : "+a);
        for(int i=0;i<a.length();i++)//97-122
        {
            if (a.charAt(i) >= 65 && a.charAt(i) <= 90) {
                int c=(int)a.charAt(i)+32;//65+32=97  66+32=98  67+32=99 68+32=100
                a.setCharAt(i,(char)c);//abcd
            }
        }
        System.out.println("Lowercase Output: "+a);
 
 
    }
}
 

Output

Original Input : ABCD
Lowercase Output: abcd
To download raw file Click Here

Basic Programs