# Lower case to upper case – An interesting fact

Problem: Given a string containing only lowercase letters, generate a string with the same letters, but in uppercase.

```Input : GeeksForGeeks
Output : GEEKSFORGEEKS ```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The first method that comes to our mind is

## C++

 `// C++ program to convert a string to uppercase ` `#include ` `using` `namespace` `std; ` ` `  `// Converts a string to uppercase ` `string to_upper(string &in) ` `{ ` `    ``for` `(``int` `i = 0; i < in.length(); i++) ` `          ``if` `(``'a'` `<= in[i] <= ``'z'``) ` `              ``in[i] = in[i] - ``'a'` `+ ``'A'``; ` `    ``return` `in; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `   ``string str = ``"geeksforgeeks"``; ` `   ``cout << to_upper(str); ` `   ``return` `0; ` `} `

## Java

 `// Java program to convert a string to uppercase ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Converts a string to uppercase ` `    ``static` `String to_upper(``char``[] in) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < in.length; i++)  ` `        ``{ ` `            ``if` `(``'a'` `<= in[i] & in[i] <= ``'z'``) ` `            ``{ ` `                ``in[i] = (``char``) (in[i] - ``'a'` `+ ``'A'``); ` `            ``} ` `        ``} ` `        ``return` `String.valueOf(in); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``String str = ``"geeksforgeeks"``; ` `        ``System.out.println(to_upper(str.toCharArray())); ` `    ``} ` `}  ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 program to convert a string to uppercase ` ` `  `# Converts a string to uppercase ` `def` `to_upper(string): ` `    ``for` `i ``in` `range``(``len``(string)): ` `        ``if` `(``'a'` `<``=` `string[i] <``=` `'z'``): ` `            ``string ``=` `(string[``0``:i] ``+` `chr``(``ord``(string[i]) ``-`  `                                        ``ord``(``'a'``) ``+` `ord``(``'A'``)) ``+` `                                        ``string[i ``+` `1``:]) ` `    ``return` `string; ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``str` `=` `"geeksforgeeks"``; ` `    ``print``(to_upper(``str``)); ` ` `  `# This code is contributed by Rajput-Ji `

## C#

 `// C# program to convert a string to uppercase ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``// Converts a string to uppercase ` `    ``static` `String to_upper(``char` `[]In) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < In.Length; i++)  ` `        ``{ ` `            ``if` `(``'a'` `<= In[i] & In[i] <= ``'z'``) ` `            ``{ ` `                ``In[i] = (``char``) (In[i] - ``'a'` `+ ``'A'``); ` `            ``} ` `        ``} ` `        ``return` `String.Join(``""``, In); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``String str = ``"geeksforgeeks"``; ` `        ``Console.WriteLine(to_upper(str.ToCharArray())); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output :

`GEEKSFORGEEKS`

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

## C++

 `// C++ program to convert a string to uppercase ` `#include ` `using` `namespace` `std; ` ` `  `// Converts a string to uppercase ` `string to_upper(string &in) ` `{ ` `    ``for` `(``int` `i = 0; i < in.length(); i++) ` `          ``if` `(``'a'` `<= in[i] <= ``'z'``) ` `              ``in[i] &= ~(1 << 5); ` `    ``return` `in; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `   ``string str = ``"geeksforgeeks"``; ` `   ``cout << to_upper(str); ` `   ``return` `0; ` `} `

## Java

 `// Java program to convert a string to uppercase ` `class` `GFG  ` `{ ` ` `  `// Converts a string to uppercase ` `static` `String to_upper(``char``[] in) ` `{ ` `    ``for` `(``int` `i = ``0``; i < in.length; i++) ` `        ``if` `(``'a'` `<= in[i] && in[i] <= ``'z'``) ` `            ``in[i] &= ~(``1` `<< ``5``); ` `    ``return` `String.valueOf(in); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String str = ``"geeksforgeeks"``; ` `    ``System.out.println(to_upper(str.toCharArray())); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## C#

 `// C# program to convert a string to uppercase ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Converts a string to uppercase ` `static` `String to_upper(``char``[] str) ` `{ ` `    ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``if` `(``'a'` `<= str[i] && str[i] <= ``'z'``) ` `            ``str[i] = (``char``)((``int``)str[i]&(~(1 << 5))); ` `    ``return` `String.Join(``""``, str); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String str = ``"geeksforgeeks"``; ` `    ``Console.WriteLine(to_upper(str.ToCharArray())); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output :

`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.

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.

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.