A simple way to convert letter-case

Source: Internet
Author: User

"Here we are going to introduce one of the simplest and fastest ways to convert the case of letters," hints: Using bit tricks "

"The following code is written in C language"

When programming we often encounter the need to convert letter case, sometimes this is the problem, sometimes to solve other problems. We should be able to think of some methods. I'm going to introduce one of the most graceful ways to solve problems with bit tricks.

This technique is very simple, that is, the ASCII value of uppercase and lowercase letters to convert them (uppercase and lowercase ASCII value difference of 32). The ASCII value of the letter 5th bit (or 6th bit, depending on how you count) to do the XOR or operation, see below:

0110000101000001

See, only the 5th place has changed.

Why is that? Maybe the person who invented the ASCII thought it was a good idea. If you write down the ASCII value of A~z, you will find that all 5th bits have a value of 1. Then the inventor thought, why not set the 5th digit of the ASCII value of A~z to 0? So it's easier for us to change the case, so that's it.

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

Now look at the following code, and we'll convert the uppercase and lowercase letters and spaces for XOR (XOR) Operations:

#include <stdio.h>intMainvoid){    Charx ='A'; Chary ='b'; printf ("Original case:-%c%c", x, y); X= x ^' '; Y= y ^' '; printf ("\n\nchanged case:-%c%c", x, y); return 0;}

Someone asked: Why the space? Because the ASCII value of the space is 32,1 left 5 bits, which is 5th bit.

Original from http://www.studyalgorithms.com/string/easiest-way-to-change-case-of-alphabets/

A simple way to convert letter-case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.