# Flip upper case with lower case

**URL:** https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013
**Category:** Beginners
**Created:** [January 10, 2020, 10:11pm UTC](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013 "2020-01-10T22:11:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Andiwnl](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Andiwnl](https://discourse.processing.org/u/Andiwnl)
#### Post date: [January 10, 2020, 10:11pm UTC](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013/1 "2020-01-10T22:11:53Z")

</div>

hello,  
I want to swap upper case letters with lower case letters. But all I can find on the internet is just how I can completely convert the word either to lower case or to upper case. what I need is : Hello -\> hELLO

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 10, 2020, 10:28pm UTC](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013/2 "2020-01-10T22:28:55Z")

</div>

Interesting.

You could go through the String using a for-loop with charAt: [https://www.processing.org/reference/String\_charAt\_.html](https://www.processing.org/reference/String_charAt_.html) (`str_.charAt(i)`)

then check the ASCII value using int. When it’s \> 90 it’s lowercase. Then use

- [https://www.processing.org/reference/String\_toUpperCase\_.html](https://www.processing.org/reference/String_toUpperCase_.html) ( `String strNewChar = str_.charAt(i)+"".toUpperCase();` )
- otherwise [https://www.processing.org/reference/String\_toLowerCase\_.html](https://www.processing.org/reference/String_toLowerCase_.html)

Build the new String from the resulting chars

Ascii [https://en.wikipedia.org/wiki/ASCII#Printable\_characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters)

Welcome to the forum!

Regards, Chrisir

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 10, 2020, 10:43pm UTC](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013/3 "2020-01-10T22:43:36Z")

</div>

> [@Andiwnl](#):
>
> What I need is : Hello → hELLO

If we print the **binary()** form of ASCII letters:

> **[binary() / Reference](https://processing.org/reference/binary_.html)**
>
> Converts an int, byte, char, or color to a String containing the equivalent binary notation. For example, the color value produced by color(0, 102, 153, 25…

```auto
println(binary('A', 8), hex('A', 2), (byte) 'A'); // 01000001 41 65
println(binary('a', 8), hex('a', 2), (byte) 'a'); // 01100001 61 97

println(binary('Z', 8), hex('Z', 2), (byte) 'Z'); // 01011010 5A 90
println(binary('z', 8), hex('z', 2), (byte) 'z'); // 01111010 7A 122

exit();

```

We’re gonna notice the only diff. between an uppercase ASCII letter from its corresponding lowercase is that the 3rd bit of the latter is `1`.

We can create a CASE\_MASK for that 3rd bit like this:  
`static final byte CASE_MASK = 1 << 5; // 0b00100000`

That is, the bit `1` is left-shifted 5 times, which is the same as `0b00100000` in binary, `0x20` in hexadecimal and `32` in decimal.

Now, in order to use that bit-mask as a case bit flipper, we’re gonna need to use the operator `^`:

```auto
static final byte CASE_MASK = 1 << 5; // 0b00100000

void setup() {
  println(binary(CASE_MASK, 8), hex(CASE_MASK, 2), CASE_MASK); // 00100000 20 32
  println();

  println(binary('A', 8), hex('A', 2), (byte) 'A'); // 01000001 41 65
  println(binary('a', 8), hex('a', 2), (byte) 'a'); // 01100001 61 97

  println(binary('Z', 8), hex('Z', 2), (byte) 'Z'); // 01011010 5A 90
  println(binary('z', 8), hex('z', 2), (byte) 'z'); // 01111010 7A 122
  println();

  println((char) ('A' ^ CASE_MASK)); // 'a'
  println((char) ('a' ^ CASE_MASK)); // 'A'
  println((char) ('Z' ^ CASE_MASK)); // 'z'
  println((char) ('z' ^ CASE_MASK)); // 'Z'

  exit();
}

```

Now you just need to create a function which flips the case of a whole String. 😉

P.S.: Alternatively we can use the space `' '` character as the mask, b/c its ASCII value is `32`: 😳  
`static final byte CASE_MASK = ' '; // same as 1 << 5`

```auto
  println((char) ('A' ^ ' ')); // 'a'
  println((char) ('a' ^ ' ')); // 'A'
  println((char) ('Z' ^ ' ')); // 'z'
  println((char) ('z' ^ ' ')); // 'Z'

  exit();

```
