Open In App

Lower case to upper case – An interesting fact

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Input : GeeksForGeeks
Output : GEEKSFORGEEKS 
Recommended Practice

The first method that comes to our mind is  

C++




// C++ program to convert a string to uppercase
#include <iostream>
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


Javascript




<script>
      // JavaScript program to convert a string to uppercase
      // Converts a string to uppercase
      function to_upper(input)
      {
        var result = new Array(input.length);
        for (var i = 0; i < input.length; i++)
          if ("a".charCodeAt(0) <= input[i].charCodeAt(0) <= "z".charCodeAt(0))
            result[i] = String.fromCharCode(
              input[i].charCodeAt(0) - "a".charCodeAt(0) + "A".charCodeAt(0)
          );
 
        return result.join("").toString();
      }
 
      // Driver code
      var str = "geeksforgeeks";
      document.write(to_upper(str));
       
      // This code is contributed by rdtank.
    </script>


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: 

C++




// C++ program to convert a string to uppercase
#include <iostream>
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


Python3




# Python3 program to convert
# a string to uppercase
 
# Converts a string to uppercase
def to_upper(s):
    for i in range(len(s)):
        if ('a' <= s[i] <= 'z'):
            s = s[0:i] + chr(ord(s[i]) & \
                            (~(1 << 5))) + s[i + 1:];
    return s;
 
# Driver code
if __name__ == '__main__':
    string = "geeksforgeeks";
    print(to_upper(string));
 
# This code is contributed by PrinciRaj1992


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


Javascript




<script>
 
// Javascript program to convert
// a string to uppercase
 
// Converts a string to uppercase
function to_upper(In)
{
    let n = In.length;
    for(let i = 0; i < In.length; i++)
        if ('a' <= In[i] && In[i] <= 'z')
            In[i] = String.fromCharCode(
                In[i].charCodeAt(0) & (~(1 << 5)));
                 
    return (In).join("");
}
 
// Driver code
let str = "geeksforgeeks";
 
document.write(to_upper(str.split("")));
 
// This code is contributed by rag2127
 
</script>


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:

C++




//C++ code to transform the string's
//latter to uppercase english alphabet.
#include<bits/stdc++.h>
using namespace std;
void transform_to_upper(string &s){
    //in-built C++ STL function
    //which transform the string
    //to uppercase
    transform(s.begin(),s.end(),s.begin(),::toupper);
    cout<<"string with uppercase latter is: "<<endl;
    cout<<s;
}
 
//driver code
int main() {
string s="GeeksForGeeks";
//function call
transform_to_upper(s);
return 0;
}
 
//this code is contributed by Machhaliya Muhammad


Java




import java.util.*;
 
public class Main {
  public static void transformToUpper(StringBuilder s)
  {
 
    // In-built Java function that transforms the string to uppercase
    s = new StringBuilder(s.toString().toUpperCase());
    System.out.println("String with uppercase letters is:");
    System.out.println(s);
  }
 
  // driver code
  public static void main(String[] args) {
    StringBuilder s = new StringBuilder("GeeksForGeeks");
 
    // function call
    transformToUpper(s);
  }
}


Python3




# Python implementation of above approach
 
# Python code to transform the string's
# letters to uppercase English alphabet.
 
def transform_to_upper(s):
    # In-built Python function
    # which transforms the string
    # to uppercase
    s = s.upper()
    print("String with uppercase letters is: ")
    print(s)
 
# Driver code
if __name__ == '__main__':
    s = "GeeksForGeeks"
    # Function call
    transform_to_upper(s)
 
 
# This code is contributed by codebraxnzt


C#




using System;
using System.Text;
 
public class MainClass {
  public static void TransformToUpper(StringBuilder s)
  {
    // In-built C# function that transforms the string to uppercase
    s = new StringBuilder(s.ToString().ToUpper());
    Console.WriteLine("String with uppercase letters is:");
    Console.WriteLine(s);
  }
 
  // driver code
  public static void Main(string[] args) {
    StringBuilder s = new StringBuilder("GeeksForGeeks");
 
    // function call
    TransformToUpper(s);
  }
}


Javascript




// JavaScript implementation of above approach
 
// JavaScript function to transform the string's
// letters to uppercase English alphabet.
 
function transformToUpperCase(s) {
  // In-built JavaScript function
  // which transforms the string
  // to uppercase
  s = s.toUpperCase();
  console.log("String with uppercase letters is: ");
  console.log(s);
}
 
// Driver code
const s = "GeeksForGeeks";
// Function call
transformToUpperCase(s);


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.

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads