beginnerString ProblemsJava

Toggle the Case of Each Character in a String

Return a new string where every uppercase letter becomes lowercase and every lowercase letter becomes uppercase.

Quick Answer

Walk through each character. If it is uppercase, convert it to lowercase; if it is lowercase, convert it to uppercase; otherwise leave it unchanged. You can use Character.toUpperCase / toLowerCase, or add and subtract 32 in ASCII. Append each converted character to build the result.

Problem Statement

Given a string, return a new string in which the case of every alphabetic character is flipped: each uppercase letter becomes lowercase and each lowercase letter becomes uppercase. Non-letter characters (digits, spaces, punctuation) stay exactly as they are.

For example, "Hello World" becomes "hELLO wORLD". The order of characters never changes — only their case.

Input: A single string s (may include letters, digits, spaces and symbols).

Output: A string with the case of every letter toggled.

Examples

Example 1
Input:  "Hello World"
Output: "hELLO wORLD"

H->h, e->E, l->L, and so on; the space is untouched.

Example 2
Input:  "Java123"
Output: "jAVA123"

The letters flip case while the digits 1, 2, 3 stay the same.

Constraints

  • 0 <= s.length() <= 10000
  • s may contain any ASCII characters; only A-Z and a-z change case

Think Before You Code

Reveal the questions to ask yourself first
  • How do you decide whether a single character is uppercase or lowercase?
  • What is the ASCII gap between the same letter's uppercase and lowercase forms?
  • What should happen to characters that are not letters at all?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Look at one character at a time. You only need to change letters — everything else is copied as-is.
Hint 2
`Character.isUpperCase(c)` and `Character.isLowerCase(c)` tell you the case; `Character.toLowerCase` / `Character.toUpperCase` do the flip.
Hint 3
Build the answer with a `StringBuilder`, appending either the toggled letter or the original character for each position.

Approach

Reveal the step-by-step approach

Process the string character by character and assemble the result.

  1. Create an empty StringBuilder.
  2. For each character c in the string:
    • If c is uppercase, append its lowercase form.
    • Else if c is lowercase, append its uppercase form.
    • Otherwise append c unchanged.
  3. Return the built string.

If you prefer raw arithmetic, remember that 'a' - 'A' == 32, so you can add 32 to make an uppercase letter lowercase and subtract 32 to go the other way.

Dry Run

Walk through the example step by step

Toggling "Java123":

index | char | classification | appended
------+------+----------------+---------
0     | J    | uppercase      | j
1     | a    | lowercase      | A
2     | v    | lowercase      | V
3     | a    | lowercase      | A
4     | 1    | not a letter   | 1
5     | 2    | not a letter   | 2
6     | 3    | not a letter   | 3
result: "jAVA123"

Solution

Reveal the full Java solution
public class ToggleCase {
    public static String toggleCase(String s) {
        StringBuilder sb = new StringBuilder(s.length());
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(Character.toLowerCase(c));
            } else if (Character.isLowerCase(c)) {
                sb.append(Character.toUpperCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(toggleCase("Hello World")); // hELLO wORLD
        System.out.println(toggleCase("Java123"));      // jAVA123
    }
}

Each character is examined once and classified as uppercase, lowercase, or neither. Using the Character helper methods keeps the logic readable and correct for the full ASCII letter range without hand-coding the 32-offset.

A StringBuilder is used instead of string concatenation in the loop so the work stays O(n) rather than repeatedly copying an immutable String.

Time: O(n) where n is the length of the stringSpace: O(n) for the resulting string

Common Mistakes

  • Using `+=` on a String inside the loop, which rebuilds the whole string each iteration.
  • Toggling digits or symbols by blindly adding 32 without first checking the character is a letter.

Edge Cases to Test

  • An empty string returns an empty string.
  • A string with no letters (like "123 !@#") is returned unchanged.
  • Already all-lowercase or all-uppercase input simply inverts fully.

Interview Follow-Ups

  • How would you toggle case in place using a char[] instead of a StringBuilder?
  • How would the ASCII 32-offset trick fail for non-English (Unicode) letters?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us