Java-toggle alphabet case in string

ecain picture ecain · Jul 5, 2015 · Viewed 21.2k times · Source

I have my code to switch the case from upper to lower and vice versa. I also have it to where it will toggle upper to lower, and lower to upper. My question is; is there a way I can get it to also include the character such as a comma or a period. For example, if I type in the string "Hello, there." I will get: "HELLO, THERE.", "hello, there" and "hELLOTHERE". How can I get it to where my third output will say "hELLO, THERE."

import java.util.*;
public class UpperLower2
{

    public static void main(String[] args)
    {
        System.out.println("Enter in a sentence:"); 
        Scanner input = new Scanner(System.in); 
        String sentence = input.nextLine();


        System.out.println("All uppercase:" + sentence.toUpperCase());
        System.out.println("All lowercase:" + sentence.toLowerCase()); 
        System.out.println("Converted String:" + toggleString(sentence)); 
        input.close(); 
     }

    public static String toggleString(String sentence)
    {
       String toggled = ""; 
       for(int i=0; i<sentence.length(); i++)
       {


           char letter = sentence.charAt(i); 

           if(Character.isUpperCase(sentence.charAt(i)))
           {
                letter = Character.toLowerCase(letter); 
                toggled = toggled + letter; 

           }
           else if(Character.isLowerCase(sentence.charAt(i)))
           {
               letter = Character.toUpperCase(letter);
               toggled = toggled + letter; 
           }

       }
       return toggled; 

   }

}

Answer

Mureinik picture Mureinik · Jul 5, 2015

If a character is neither upper case nor lowercase, you should just take it as is. Also, don't use a String to accumulate your output - this is what StringBuilders are for:

public static String toggleString(String sentence) {
    StringBuilder toggled = new StringBuilder(sentence.length());
    for (char letter : sentence.toCharArray()) {
        if(Character.isUpperCase(letter)) {
            letter = Character.toLowerCase(letter);
        } else if(Character.isLowerCase(letter)) {
            letter = Character.toUpperCase(letter);
        }

        toggled.append(letter);

    }
    return toggled.toString();
}

EDIT:
A similar implementation in Java 8 semantics, without having to loop over the string yourself:

public static String toggleStringJava8(String sentence) {
    return sentence.chars().mapToObj(c -> {
        if (Character.isUpperCase(c)) {
            c = Character.toLowerCase(c);
        } else if (Character.isLowerCase(c)) {
            c = Character.toUpperCase(c);
        }
        return String.valueOf((char)c);
    }).collect(Collectors.joining());
}