Changing String into altCase

Viewed 9494

I am trying to write code which will change given string into alt case.

"hello" ---> "HeLlO"
"legend of zelda" ---> "LeGeNd oF ZeLdA"

This is what I have so far:

public String altCase(String text) {
    String replace = "";

    for (int i = 0; i < text.length(); i++) {
        if (i % 2 == 0) {
            replace = text.replace(Character.toString(text.charAt(i)),
                    Character.toString(text.charAt(i)).toLowerCase());
        } else {
            replace = text.replace(Character.toString(text.charAt(i)),
                    Character.toString(text.charAt(i)).toUpperCase());
        }
    }

    return replace;
}

replace turns into text and nothing else changes.

What is causing this, and how do I fix it?

2 Answers
Related