I Spy Code - Java

Swap case in a string

Question:

Write a java program that swaps the case in a string. Change upper and title case to lower case, and lower case to upper case.

Solution:

Here is a java example that swaps the case of each character in a string:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
    public static void main(String[] args) {
        String str1 = "The Cat In The Hat";
        System.out.println(str1);
 
        String str2 = StringUtils.swapCase(str1);
        System.out.println(str2);
    }
}
 

Output:

$ java Example
The Cat In The Hat
tHE cAT iN tHE hAT

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#swapCase-java.lang.String-

Questions answered by this page:

Java example that switches case in a string