C - Use binary OR operator to change character case

Description

Use binary OR operator to change character case

Demo

#include <stdio.h> 

int main() /*from w  w  w  . j av  a 2s. co  m*/
{ 
   char input[64]; 
   int ch; 
   int x = 0; 

   printf("Type in ALL CAPS: "); 
   fgets(input,63,stdin); 

   while(input[x] != '\n') 
   { 
       ch = input[x] | 32; 
       putchar(ch); 
       x++; 
   } 
   putchar('\n'); 

   return(0); 
}

Result

Because of the way the ASCII codes map between upper- and lowercase characters, you can switch the case by simply setting the sixth bit in a byte.

Related Topic