Home Strings Easiest way to change case of alphabets

# Easiest way to change case of alphabets

Here we will discuss the easiest and most simple way to change case of alphabets. Hint : We will be using bit hacks.

Often during our programming needs we need to change the case of alphabets. This can be a problem requirement or simply our need to solve the question. There are several ways to do it, but what can be more beautiful than using bit hacks for the same.
We discusses some of the BIT Hacks in these posts:-

Here I want to discuss the ASCII trick to convert lowercase to uppercase and back. The trick is super simple, you xor the 6th bit (or 5th depending on how you count bits) and that changes the case!
Check this out:


a = 01100001
A = 01000001


See, just the 6th bit changed.

Why is it this way? Simply because the people who invented ASCII thought it was a great idea. If you look at characters a..z, you’ll see that all of them have the 6th bit set to 1. The ASCII inventors though, hey, let’s set 6th bit to 0 for upper case letters A..Z, then it will be super easy to change case. So they did.


a = 01100001    A = 01000001
b = 01100010    B = 01000010
c = 01100011    C = 01000011
d = 01100100    D = 01000100
e = 01100101    E = 01000101
f = 01100110    F = 01000110
g = 01100111    G = 01000111
h = 01101000    H = 01001000
i = 01101001    I = 01001001
j = 01101010    J = 01001010
k = 01101011    K = 01001011
l = 01101100    L = 01001100
m = 01101101    M = 01001101
n = 01101110    N = 01001110
o = 01101111    O = 01001111
p = 01110000    P = 01010000
q = 01110001    Q = 01010001
r = 01110010    R = 01010010
s = 01110011    S = 01010011
t = 01110100    T = 01010100
u = 01110101    U = 01010101
v = 01110110    V = 01010110
w = 01110111    W = 01010111
x = 01111000    X = 01011000
y = 01111001    Y = 01011001
z = 01111010    Z = 01011010


Also check this out, if you xor a character with a space, you invert the case:

#include<stdio.h>
int main(void)
{
char x = 'A';
char y = 'b';

printf("Original case:- %c %c",x,y);

x = x ^ ' ';
y = y ^ ' ';

printf("\n\nChanged case:- %c %c",x,y);

return 0;
}


Why is that? Because a space ‘ ‘ has the value 32, which is 1<<5, which is the 6th bit, which swaps the case!

Here is the Ideone link for the running code:- http://ideone.com/K25YEJ

#### You may also like July 14, 2015 - 19:09

Find largest two elements of the given array July 5, 2015 - 17:57

Please give tutorials on dynamic programming and involve lots of different types of examples…..thank you nikoo. July 5, 2015 - 20:34

I will be starting with dynamic programming soon. I am first covering the basics so that I can use them later on while explaining dynamic programming concepts.
Thanks for your support and patience. May 17, 2015 - 20:36

Interesting..

From the above example, it seems we just need to make 6th bit 0. So we don’t need xor? May 20, 2015 - 10:37

You don’t need to make the 6th bit 0, you need to toggle the 6th bit to change the case. May 7, 2015 - 21:12

Really a nice one ! Atleast it’s new to me ;) May 20, 2015 - 10:38

Please list what more algorithms would you like.