Java Program to Change Case of a Character/String

Displaying Opposite Character: 

Logic:

  • First we should have to find the given case, for this you can use Character.isLowerCase(given character) or Character.isUpperCase(given character).
  • Once you know the given character then you can change to opposite character using Character.toLowerCase(given input) or Character.toUpperCase(given input).

Program: [This program converts to opposite case – Character]

package in.javadomain;

public class ChangeToOppositeCharacter {

	public static void main(String[] args) {
		Character c = 'd';
		if (Character.isLowerCase(c)) {
			System.out.println("Given Case :  " + c);
			System.out.println("Opposite Case :  " + Character.toUpperCase(c));
		} else {
			System.out.println("Given Case :  " + c);
			System.out.println("Opposite Case :  " + Character.toLowerCase(c));
		}
	}
}

Output:

Given Case :  d
Opposite Case :  D

 

Program: [Upper case Input, Lower Case Output]

package in.javadomain;

public class ChangeToOppositeCharacter {

	public static void main(String[] args) {
		Character c = 'N';
		if (Character.isLowerCase(c)) {
			System.out.println("Given Case :  " + c);
			System.out.println("Opposite Case :  " + Character.toUpperCase(c));
		} else {
			System.out.println("Given Case :  " + c);
			System.out.println("Opposite Case :  " + Character.toLowerCase(c));
		}
	}
}

Output:

Given Case :  N
Opposite Case :  n

 

Changing the String Case to Opposite Case: [This program converts to opposite case – String, but returns Character by character]
The above code separated as method and the method called repeatedly.

 

package in.javadomain;

public class ChangeToOppositeString {

	public static void main(String[] args) {
		char oppositeChar = 0;
		String input = "Javadomain.In";
		for (int i = 0; i < input.length(); i++) {
			CharSequence val = input.subSequence(i, i + 1);
			oppositeChar = oppositeChar(val.charAt(0));
			System.out.println(oppositeChar);
		}

	}

	private static char oppositeChar(char c) {
		char oppoChar = 0;
		if (Character.isLowerCase(c)) {
			oppoChar = Character.toUpperCase(c);
		} else {
			oppoChar = Character.toLowerCase(c);
		}
		return oppoChar;
	}
}

Output:

j
A
V
A
D
O
M
A
I
N
.
i
N

 

Printing Opposite Case of a String: [This program converts to opposite case – String]
The above returns the opposite case of a character. If you want the whole word to be returned after the case change, then you can go with the below program.

package in.javadomain;

public class StringOppositeCase {

	public static void main(String[] args) {
		String input = "I Love Javadomain.in";
		System.out.println("Given Input ::: " + input);
		StringBuilder strBuilder = new StringBuilder(input);
		for (int i = 0; i < strBuilder.length(); i++) {
			char c = strBuilder.charAt(i);
			if (Character.isUpperCase(c)) {
				strBuilder.setCharAt(i, Character.toLowerCase(c));
			} else {
				strBuilder.setCharAt(i, Character.toUpperCase(c));
			}

		}
		System.out.println("Opposite Case::: " + strBuilder);

	}

}

Output:

Given Input ::: I Love Javadomain.in
Opposite Case::: i lOVE jAVADOMAIN.IN

Recommended Books:

989 total views, 0 views today

Leave a Reply

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