Home / Blue J / Java program to change lower case to upper case and vice versa

Java program to change lower case to upper case and vice versa

This is a program to alter the case of all characters in a string given as a input. So small letter becomes capital and vice-versa, no change to other symbols or numbers is made. Thus upper case changes to lower case and lower to upper.

Example:- HellO WoRlD

OUTPUT:- hELLo wOrLd

import java.io.*;
class prog5
{
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter the string");
String s = br.readLine();
int i,l;
char ch;
l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(Character.isUpperCase(ch))
System.out.print(Character.toLowerCase(ch));
else if(Character.isLowerCase(ch))
System.out.print(Character.toUpperCase(ch));
else
System.out.print(ch);
}
}
}

 

OUTPUT:

program to change lower case to upper case and vice versa

Tags: change character case , program to change lower case to upper case and vice versa

Also See:   Java program for armstrong number using recursion

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

4 comments

  1. Thank you for this. A similar question was asked to me in a labtest. The only difference was to print out numbers as well. for example if the input was: AbCd123 , output should be: aBcD123
    How can I print out the numbers as well?

  2. how can we change only the first letter to uppercase and the others to lowercase
    Eg:jJACK IS A DuLl bOy = input
    :Jack Is A Dull Boy

Leave a Reply

Your email address will not be published. Required fields are marked *