VOOZH about

URL: https://www.geeksforgeeks.org/dsa/lower-case-upper-case-interesting-fact/

⇱ Lower case to upper case - An interesting fact - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lower case to upper case - An interesting fact

Last Updated : 23 Jul, 2025

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  


Output
GEEKSFORGEEKS

Time Complexity: O(n),where n is length of string.

Auxiliary Space: O(1)


A more interesting solution, on the other hand, would be: 


Output
GEEKSFORGEEKS

Time Complexity: O(n),where n is length of string.

Auxiliary Space: O(1)

Method 3(using C++ STL)

In C++ STL there is a function called "transform" which converts the case of all latter of a string.

Below is an implementation of that function:


Output
string with uppercase latter is: 
GEEKSFORGEEKS

Time Complexity: O(n),where n is length of string.

Auxiliary Space: O(1)


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: 

  1. Write a function to make all letters of a string lowercase. Example: GeeksForGeeks turns geeksforgeeks.
  2. Write a function that change the case of a string. Example: GeeksForGeeks turns gEEKSfORgEEKS.


 

Comment
Article Tags:
Article Tags: