Java Program to Move All Uppercase Characters to the End
In this tutorial, we are going to learn about moving all uppercase characters to the end in Java. Given a string with both lowercase and uppercase characters in it. Moving every capital character to the end of the String is the work at hand. The sequence of the uppercase characters ought to match that of the original string.
Examples:
Example 1:
Input: "heLLGFg"
Output: "hegLLGF"
Explanation: Upperacase alphabets are moved to the end in a given string.
Example 2:
Input: "HeLLo"
Output: "eoHLL"
Explanation: Upperacase alphabets are moved to the end in a given string.
Approach 1: Using ASCII values of the characters
ALGORITHM:
Step 1: Check the ASCII values of each character in the string as you iterate over the characters one by one.
Step 2: ASCII values fall under [65-90] char values for all uppercase characters. At the conclusion of the loop, output the string that contains the characters from these ASCII values that you stored in the String variable.
Filename: UppercaseCharacters.java
import java.io.*; public class UppercaseCharacters { // Method 1 To change the capitalization of characters static void moveUppercaseToEnd(String x, int length) { // Using a blank string String uppercaseCharacters = ""; for (int i = 0; i < length; ++i) { // Check the condition to see if the character is uppercase using the character's ASCII values. if (x.charAt(i) >= 65 && x.charAt(i) <= 90) { uppercaseCharacters += x.charAt(i); } // The character is already written in lowercase. else System.out.print(x.charAt(i)); } // Printing the capital letter string now System.out.print(uppercaseCharacters); } public static void main(String[] args) { // personalized input string String x = "heLLo"; // Finding the string's length using the length() method int length = x.length(); // Moving every capital character to the end of the custom string taken above requires calling method 1 over it. moveUppercaseToEnd(x, length); } }
Output:
heoLL
Complexity analysis:
Time complexity: The time complexity of the program is O(n), where n is the length of the string.
Space complexity: The space complexity of the program is O(1).
Approach 2: Using queue data structures
ALGORITHM:
Step 1: Use the First in First Out principle, which the Queue Data Structure provides, we store the capital letters in the queue.
Step 2: Print every character, omitting the capital letters.
Step 3: Follow the iteration, every entry in the queue is eliminated.
Filename: UppercaseCharacters.java
import java.io.*; import java.util.*; public class UppercaseCharacters { static void moveUppercaseToEnd(String x, int length) { // Building a character / queue class object of type QueueQ = new LinkedList (); // Declaring a string initialization to be empty String uppercaseCharacters = ""; for (int i = 0; i < length; ++i) { // Verify the condition of the capital letters Use the ASCII values of the ~~~// character if it is uppercase. if (x.charAt(i) >= 65 && x.charAt(i) <= 90) { Q.add(x.charAt(i)); } // Character is lowercase else // Leave it ascites on its index System.out.print(x.charAt(i)); } // The uppercase string is now printed until there are elements in the queue. while (Q.size() != 0) { // Eliminating every item from the queue System.out.print(Q.peek()); // get the queue cleared Q.remove(); } } public static void main(String[] args) { // the Given input string String x = "heLLo"; // Using the length() method to calculate the string's length int length = x.length(); moveUppercaseToEnd(x, length); } }
Output:
heoLL
Complexity analysis:
Time complexity: The time complexity of the program is O(N), where n is the length of the string.
Space complexity: The space complexity of the program is O(n).