C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Reverse Uppercase & Lowercase in String Program - Java Programs

Write a program to input a string and print out the text with the uppercase and lowercase letters reversed, but all other characters should remain the same as before.
Example:
INPUT: WelComE TO School
OUTPUT: wELcOMe to sCHOOL

import java.util.Scanner; class Reverse{ public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.print("Enter the string: "); String s = in.nextLine(); String t = ""; for(int i = 0; i < s.length(); i++){ char ch = s.charAt(i); if(Character.isUpperCase(ch)) t += Character.toLowerCase(ch); else t += Character.toUpperCase(ch); } System.out.println(t); } }

Output

 
 OUTPUT  : 
Enter the string: WelComE TO School
wELcOMe to sCHOOL