Convert the given string into uppercase in Java


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

  • The program starts by initializing a StringBuilder object a with the string "abc", 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 a lowercase letter (its ASCII value is between 97 and 122), it is converted to uppercase by subtracting 32 from 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("Uppercase Output: "+a).

Source Code

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

Output

Original Input    : abc
Uppercase Output  : ABC
To download raw file Click Here

Basic Programs