Lower case to upper case - An interesting fact - GeeksforGeeks
Explanation: The ASCII table is constructed in such way that the binary representation of lowercase letters is almost identical of binary representation of uppercase letters. The only difference is the sixth bit, setted only for lowercase letters. What that elegant function does is unset the bit of index 5 of in[i], consequently, making that character lowercase.
Disadvantages: That strategy works only for alphabetical characters. If the input contains numbers or punctuations, we'll have to use the naive way.
Example: Character 'A' is integer 65 = (0100 0001)2, while character 'a' is integer = 97 = (0110 0001)2. (Note that 97 – 65 = 32. Can you guess why?)
Exercises:
- Write a function to make all letters of a string lowercase. Example: GeeksForGeeks turns geeksforgeeks.
- Write a function that change the case of a string. Example: GeeksForGeeks turns gEEKSfORgEEKS.
Read full article from Lower case to upper case - An interesting fact - GeeksforGeeks
No comments:
Post a Comment