Problem: Given a string containing only lowercase letters, generate a string with the same letters, but in uppercase.
Input : GeeksForGeeks Output : GEEKSFORGEEKS
The first method that comes to our mind is
A more interesting solution, on the other hand, would be:
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?)
- 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.
This article is contributed by Igor Carpanese. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. 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
- String in Switch Case in Java
- Convert characters of a string to opposite case
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Toggle case of a string using Bitwise Operators
- Permute a string by changing case
- Camel case of a given sentence
- Snake case of a given sentence
- Alternate Lower Upper String Sort
- gOOGLE cASE of a given sentence
- Students with maximum average score of three subjects
- Check if words are sorted according to new order of alphabets
- Smallest Greater (than S) String of length K whose letters are subset of S
- Reverse middle words of a string
- Move all special char to the end of the String