Given a string that contains uppercase letters as well as lower case letters. The task is to move all uppercase characters at the end of the String. The uppercase characters must be in the same order as in the original string.
Input : "heLLGFg" Output : "hegLLGF" Input : "Hello" Output : "elloH"
Here we are having two different approaches to get to the problem namely as follows:
- Using ASCII values of the characters.
- Using queue data structures
Approach 1: Using ASCII values of the characters.
- Iterate the character of the string one by one and checks the ASCII values of the respective character of the string.
- For all uppercase letters, ASCII values lie under [65-90] char values. Store the characters of these ASCII values in the String variable and print the string at the end of the Iteration.
Example:
Java
// Java Program to Move All Uppercase Characters to the EndÂ
// Importing input output classesimport java.io.*;Â
// Mai classclass GFG {Â
    // Method 1    // To shift uppercase characters    static void shiftuppercase(String m, int length)    {Â
        // Taking an empty string        String temp = "";Â
        for (int i = 0; i < length; ++i) {Â
            // Condition check            // If the character is uppercase via            // the ASCII values of the character            if (m.charAt(i) >= 65 && m.charAt(i) <= 90) {                temp += m.charAt(i);            }Â
            // The character is already lowercase            else                System.out.print(m.charAt(i));        }Â
        // Now, Printing the uppercase string        System.out.print(temp);    }Â
    // Method 2    // Main driver method    public static void main(String[] args)    {        // Custom input string        String m = "heLLGFg";Â
        // Computing the length of the string        // using length() method        int length = m.length();Â
        // Calling the method 1 over the custom string taken        // above to move all uppercase char to the end        shiftuppercase(m, length);    }} |
Output:
Time Complexity: O(n) //n is the length of the string.
Space Complexity: O(1)
Approach 2: Using queue data structures  Â
- Queue Data structure implements the First in First out concept, We use this concept and store the uppercase characters in the queue.
- Print all the characters except the uppercase characters.
- After the iteration removes all the elements from the queue.
Example:Â
Java
// Java Program to Move All Uppercase Characters to the End// Using Queue data structuresÂ
// Importing input output classesimport java.io.*;// Importing utility classesimport java.util.*;Â
// Main classclass GFG {Â
    // Method 1    // Main driver method    static void shiftuppercase(String m, int length)    {        // Creating an object of Queue class of character        // type        Queue<Character> Q = new LinkedList<Character>();Â
        // Declaring an initializing to empty string        String temp = "";Â
        for (int i = 0; i < length; ++i) {Â
            // Condition checkfor the uppercase characters            // If uppercase use ASCII values of the            // character            if (m.charAt(i) >= 65 && m.charAt(i) <= 90) {                Q.add(m.charAt(i));            }Â
            // Character is lowercase            elseÂ
                // Leave it ascites on its index                System.out.print(m.charAt(i));        }Â
        // Now, printing the uppercase string till        // there are elements in queue        while (Q.size() != 0) {Â
            // Removing all the elements from the queue            System.out.print(Q.peek());Â
            // Clear the queue            Q.remove();        }    }Â
    // Method 2    // main driver method    public static void main(String[] args)    {Â
        // Given input string        String m = "heLLGFg";Â
        // Computing the length of the string        // using length() method        int length = m.length();Â
        // Calling the        shiftuppercase(m, length);    }} |
Output:Â
Time Complexity: O(N) // where N is the length of the given string.
Auxiliary Space: O(n)
Approach 3 : Using isUpperCase() methodÂ
Java
// Java Program to Move All Uppercase Characters// to the EndÂ
// Importing input output classesimport java.io.*;Â
// Main classclass GFG {Â
    public static void main(String[] args)    {        // Custom input string        String m = "heLLGFg";        String newstr = "";        String upper = "";        for (int i = 0; i < m.length(); i++) {            if (Character.isUpperCase(m.charAt(i))) {                upper += m.substring(i, i + 1);            }            else {                newstr += m.substring(i, i + 1);            }        }        System.out.println(newstr + upper);    }} |
hegLLGF

