3

I have a String called "originalstring" which contains a sentence with mixed upper and lower case characters.

I simply want to flip the string so that if a character is a lowercase make it upper case and vice versa and return it.

I have tried this code, which returns the original string in upperCase:

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

        if (Character.isUpperCase(c)) {
            originalString += Character.toLowerCase(c);

        }

        if (Character.isLowerCase(c)) {
            originalString += Character.toUpperCase(c);

        }

    }
    return originalString;
me72921
  • 173
  • 2
  • 10
  • `+=` to convert?? That's odd –  May 04 '15 at 19:03
  • Is this your actual code? Looks like an infinite loop to me at a glance... – CupawnTae May 04 '15 at 19:04
  • How could this possibly be an infinite loop? You need an else statement. Your first `if` converts uppercase letters to lower case letters. It then imminently goes into your second `if` statement, and converts it to an upper case. Just switch the second `if` to an `else`. Id also recommend using a StringBuilder. – user489041 May 04 '15 at 19:05
  • Probable duplicate of http://stackoverflow.com/questions/1729778/how-can-i-invert-the-case-of-a-string-in-java – Mohammed May 04 '15 at 19:06
  • @user489041 because it adds to `originalString` without removing characters, and `originalString.length()` is in the loop termination condition. Of course it would eventually run out of memory. – CupawnTae May 04 '15 at 19:15

3 Answers3

7

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();
rgettman
  • 176,041
  • 30
  • 275
  • 357
6

Common-lang provide a swapCase function, see the doc. Sample from the doc:

StringUtils.swapCase(null)                 = null
StringUtils.swapCase("")                   = ""
StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"

And if you really want to do it by yourself, you can check the source of common-lang StringUtils

0

Instead of using existing utilities, you may try below conversion using boolean operation:

To upper case:

 char upperChar = (char) (c & 0x5f)

To lower case:

   char lowerChar = (char) (c ^ 0x20)

In your program:

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

            if (Character.isUpperCase(c)) {
                result.append((char) (c ^ 0x20));

            }
            else if ((c >= 'a') && (c <= 'z')) {
                result.append((char) (c & 0x5f));

            }
            else {
                result.append(c);
            }

        }
        System.out.println(result);
Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91