How to Invert the Case of a String in Java

How can I invert the case of a String in Java?

I don't believe there's anything built-in to do this (it's relatively unusual). This should do it though:

public static String reverseCase(String text)
{
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++)
{
char c = chars[i];
if (Character.isUpperCase(c))
{
chars[i] = Character.toLowerCase(c);
}
else if (Character.isLowerCase(c))
{
chars[i] = Character.toUpperCase(c);
}
}
return new String(chars);
}

Note that this doesn't do the locale-specific changing that String.toUpperCase/String.toLowerCase does. It also doesn't handle non-BMP characters.

Reverse a string in Java

You can use this:

new StringBuilder(hi).reverse().toString()

StringBuilder was added in Java 5. For versions prior to Java 5, the StringBuffer class can be used instead — it has the same API.

How to invert the case of a single character?

Use Character.isUpperCase() to determine what case the character is, and Character.toLowerCase() and Character.toUpperCase() to change the case.

char c = 'A';
char result = Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c);
System.out.println(result);

Which prints:

a

Java-Program to reverse upper case and lower case

The issue is that 32 is an integer, and let is a char. Java will implicity convert the let value to an int when it encounters let-32 and the result is the int value (for 'a') 96 or whatever.

You need to cast the result back to char:

(char)(let+32)

Convert java char inside string to lowerCase/upperCase

You are adding characters to the original string. Also, this means that your for loop will never get to the end of the iteration of the for loop, because originalString.length() changes each loop also. It's an infinite loop.

Instead, create a StringBuilder that stores the converted characters as you're iterating over the original string. The convert it to a String and return it at the end.

StringBuilder buf = new StringBuilder(originalString.length());
for (int i = 0; i < originalString.length(); i++) {
char c = originalString.charAt(i);

if (Character.isUpperCase(c)) {
buf.append(Character.toLowerCase(c));

}
else if (Character.isLowerCase(c)) {
buf.append(Character.toUpperCase(c));

}
// Account for case: neither upper nor lower
else {
buf.append(c);
}

}
return buf.toString();

Inverse string case

You have just about everything correct, except for inside of your if statement. You're setting n equal to its toLowerCase or toUpperCase method, not its return value. You need to call those methods and set n equal to their return values:

var convertString = function (str) {
var s = '';
var i = 0;
while (i < str.length) {
var n = str.charAt(i);
if (n == n.toUpperCase()) {
// *Call* toLowerCase
n = n.toLowerCase();
} else {
// *Call* toUpperCase
n = n.toUpperCase();
}

i += 1;
s += n;
}
return s;
};

convertString("lower UPPER");

The output that you're getting ('function toUpperCase() { [native code] }...) is the result of each method being converted to a string, then concatenated to your result string. You can achieve the same result by running either of these commands in your console:

  • console.log("".toUpperCase);
  • console.log("".toUpperCase.toString());

The results of both are function toUpperCase() { [native code] }.

Happy coding!



Related Topics



Leave a reply



Submit