Convert the given string into toggle case word in Java


This Java program implements a toggle case feature on a given string. The program starts by initializing a StringBuilder object a with the string "Tutor Joes Computer Education", and it is printed using System.out.println("Original String : "+a).

Then, a for loop is used to iterate through all characters of the string. If the character is lowercase (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. Otherwise, if the character is uppercase (its ASCII value is between 65 and 90), it is converted to lowercase by adding 32 to its ASCII value and setting the new character using the setCharAt() method.

Finally, the updated string is printed using System.out.println("tOGGLE cASE wORD Output : "+a).

Source Code

public class toogle {
    public static void main(String args[]) {
        //Program convert the given string into tOGGLE cASE wORD
        StringBuilder a = new StringBuilder("Tutor Joes Computer Education");
        System.out.println("Original String : "+a);
 
        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) >= 97 && a.charAt(i) <= 122) {
                int c = (int) a.charAt(i) - 32;
                a.setCharAt(i, (char) c);
            } else if (a.charAt(i) >= 65 && a.charAt(i) <= 90) {
                int c = (int) a.charAt(i) + 32;
                a.setCharAt(i, (char) c);
            }
        }
        System.out.println("tOGGLE cASE wORD Output : "+a);
    }
}
 

Output

Original String : Tutor Joes Computer Education
tOGGLE cASE wORD Output : tUTOR jOES cOMPUTER eDUCATION
To download raw file Click Here

Basic Programs