Lower Case To Upper Case

Difficulty Level Easy
Frequently asked in Adobe Apple Google
StringViews 2094

Problem Statement

In the “Lower Case To Upper Case” problem, we have given a string “s” with only lower case letters. Write a program that will print the same string but with upper case letters.

Input Format

The first and only one line containing a string “s”.

Output Format

The first and only one line containing a string that contains the upper-case English alphabet only.

Constraints

  • 1<=|s|<=10^6
  • s[i] must be a lower case English alphabet

Example

tutorialcup
TUTORIALCUP

Algorithm

  1. Traverse the string character by character.
  2. For every character in the string, do s[i] – ‘a’ + ‘A’
  3. Once the traversal complete then print the updated string.

Note: The ASCII table is constructed in such a way that the binary representation of lowercase letters is almost identical of the binary representation of uppercase letters. The only difference is the sixth bit, setted only for lowercase letters.

Implementation

C++ Program for Lower Case To Upper Case

#include <bits/stdc++.h>
using namespace std;
int main()
{
   string s;
   cin>>s;
   int n=s.length();
   for(int i=0;i<n;i++)
   {
       s[i]= s[i]-'a'+'A';
   }
   cout<<s<<endl;
   return 0;
}

Java Program for Lower Case To Upper Case

import java.util.Scanner;

class sum { 
  public static void main(String[] args) 
        {
            Scanner sr = new Scanner(System.in);
            String s = sr.next();
            int n =s.length();
            char a[] = s.toCharArray();
            String ans="";
            for(int i=0;i<n;i++)
            {
                a[i]= (char) (a[i]-'a'+'A');
                ans+=a[i];
            }
      System.out.println(ans); 
  } 
}
google
GOOGLE

Complexity Analysis for Lower Case To Upper Case

Time Complexity

O(n) where n is the size of the given string “s”.  Here we simply traverse the string and change the lower case to upper_case by constant time operation. So our time complexity will be linear.

Space Complexity

O(1) because we only update the given string and print it. Here we don’t create any auxiliary space.

Translate »