Header Ads

25 - WAP to Print Upper Case & Lower Case using Bitwise in C

This Program prints the given string in both Upper Case & Lower Case, this is just a simple basic code using bitwise operator's. (AND & OR)

#include <stdio.h>
#include <string.h>

//Global declaration of variables
const int x = 32;
int idx;

//Calling of Uppercase conversion function
char *uppercase(char *str)
{
 //Bitwise ANDing (&) and then negation (~) of 32 letters to obtain Upper case.
 for (idx=0; str[idx]!= '\0'; idx++)
  str[idx] = str[idx] & ~x;
 printf("String in Upper Case : %s\n", str);
}

//Calling of Lowercase conversion function
char *lowercase(char *str)
{
 //Bitwise ORing (&) with that of 32 letters to obtain lower case.
 for (idx=0; str[idx]!= '\0'; idx++)
  str[idx] = str[idx] | x;
 printf("String in Lower Case : %s\n", str);
}

// Main Function Program
int main()
{
 char str[100];
 printf("Enter a String : ");
 scanf("%s", str);
 //Printing the String in Upper Case 
 uppercase(str);
 lowercase(str);
 return 0; 
}  

Program Output:

No comments