Home >>Java Programs >Java Program to replace lowercase characters by uppercase and vice-versa
In this example, we will create a java program to replace all the lower-case characters in the string to upper-case and upper-case characters to lower-case.
public class Main
{
public static void main(String[] args)
{
String str1="Abhimanyu";
StringBuffer newStr=new StringBuffer(str1);
for(int i = 0; i < str1.length(); i++)
{
if(Character.isLowerCase(str1.charAt(i)))
{
newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i)));
}
else if(Character.isUpperCase(str1.charAt(i)))
{
newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i)));
}
}
System.out.println("String after case conversion : " + newStr);
}
}