Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomize capital letters

Is there a simple way to, given a word String, randomise capital letters?

Example:

For word super I would get SuPEr or SUpER.

I am looking for a Java solution for this.

like image 967
Tiago Veloso Avatar asked May 23 '26 00:05

Tiago Veloso


1 Answers

Here is one suggestion:

public static String randomizeCase(String str) {

    Random rnd = new Random();
    StringBuilder sb = new StringBuilder(str.length());

    for (char c : str.toCharArray())
        sb.append(rnd.nextBoolean()
                      ? Character.toLowerCase(c)
                      : Character.toUpperCase(c));

    return sb.toString();
}

Example

input: hello world
output: heLlO woRlD

(ideone.com demo)

like image 148
aioobe Avatar answered May 24 '26 13:05

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!