![]() |
VOOZH | about |
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
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:
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:
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: