Toggle case of a string using Bitwise operators

3.6

Given a string, write a function that returns toggle case of a string using the bitwise operators in place.

In ASCII codes character ‘A’ is integer 65 = (0100 0001)2, while character ‘a’ is integer 97 = (0110 0001)2.

The difference between the ASCII values of ‘a’ and ‘A’ is 32.

So we can easily change the case of the letters either from Upper to lower or lower to upper by adding or subtracting the difference from the letters.

Examples:

Input : "GeekSfOrgEEKs"
Output : "gEEKsFoRGeekS"                  

Input : "StRinG"
Output : "sTrINg"

The ASCII table is constructed in such way that the binary representation of lowercase letters is almost identical of binary representation of uppercase letters.

1. Lower case to CAPS
Bitwise XORing of a lowercase letter with a value of 65 (ASCII of ‘A’) or 90 (ASCII of ‘Z’) returns a value from 32 to 59, which upon bitwise ANDing with 32 always returns a nonzero of 32. So we convert it to uppercase.

2. CAPS to Lower case
Bitwise XORing of a uppercase letter with a value of 65 (ASCII of ‘A’) or 90 (ASCII of ‘Z’) returns a value from 0 to 27, which upon bitwise ANDing with 32 always returns a zero. So we convert it to lowercase as above.

C

// C program to get toggle case of a string
#include <stdio.h>
const int x=32;

// tOGGLE cASE = swaps CAPS to lower
// case and lower case to CAPS
char *toggleCase(char *a)
{
    for (int i=0; a[i]!='\0'; i++){

        // We can also do a[i]^90
        if ((a[i]^65) & x)
            a[i] &= ~x;
        else
            a[i] |= x;
    }

    return a;
}

// Driver Code
int main()
{
    char str[] = "CheRrY";
    printf("Toggle case: %s\n", toggleCase(str));
    printf("Original string: %s", toggleCase(str));
    return 0;
}

Java

// program to get toggle case of a string

public class Test
{
	
	static int x=32;
	
	// tOGGLE cASE = swaps CAPS to lower
	// case and lower case to CAPS
	static String toggleCase(char[] a)
	{
	    for (int i=0; i<a.length; i++){
	 
	        // We can also do a[i]^90
	    	int y = (a[i]^65) & x;
	        if (y!=0)
	            a[i] &= ~x;
	        else
	            a[i] |= x;
	    }
	    return new String(a);
	}
	
    /* Driver program */
    public static void main(String[] args) 
    {
    	String str = "CheRrY";
    	System.out.print("Toggle case: ");
    	str = toggleCase(str.toCharArray());
    	System.out.println(str);
    	
    	System.out.print("Original string: ");
    	str = toggleCase(str.toCharArray());
    	System.out.println(str);	
    }
}


Output:

Toggle case: cHErRy
Original string: CheRrY

Similar Article :
Case conversion of a string using BitWise operators in C/C++

This article is contributed by Sanjay Kumar Ulsha from JNTUH College Of Engineering, Hyderabad. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.6 Average Difficulty : 3.6/5.0
Based on 6 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.