Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-toggle alphabet case in string

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; 

   }

}

like image 975
ecain Avatar asked Jan 20 '26 15:01

ecain


2 Answers

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());
}
like image 146
Mureinik Avatar answered Jan 23 '26 03:01

Mureinik


Given the source code that you posted, you now have an if-statement with two branches: one for the case where the character was upper-case and one when the character was lower-case. Characters like comma and other punctuation symbols don't have upper or lower-case, so they are ignored by your if-statement and else-block.

To work around that, add another else block to the statement:

else {
    toggled = toggled + letter; 
}

After you have that working, you should look into making your code cleaner.

You now have the statement toggled = toggled + letter; three times in your code; you can change that into one time:

       char letter = sentence.charAt(i); 

       if(Character.isUpperCase(sentence.charAt(i)))
       {
            letter = Character.toLowerCase(letter); 
       }
       else if(Character.isLowerCase(sentence.charAt(i)))
       {
           letter = Character.toUpperCase(letter);
       }
       // else {
       // }
       // You can remove the latest `else` branch now, because it is empty.

       toggled = toggled + letter; 

Also, the preferred way to build strings in Java is using StringBuilder instead of the + operator on strings. If you search on StackOverflow for StringBuilder you'll get plenty of examples on how to use that.

like image 36
Erwin Bolwidt Avatar answered Jan 23 '26 04:01

Erwin Bolwidt